Day91 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which we will connect our app to the internet.
Unit 4: Internet
Pathway 2: Get and Display Data
https://developer.android.com/courses/android-basics-kotlin/course
Introduction to HTTP/REST
We will use the Retrofit library to talk to the Mars web service and display the raw JSON response as a String
. The placeholder TextView
will either display the returned JSON response string or a message indicating a connection error.
Retrofit creates a network API for the app based on the content from the web service. It fetches data from the web service and routes it through a separate converter library that knows how to decode the data and return it in the form of objects like String
. Retrofit includes built-in support for popular data formats such as XML and JSON. Retrofit ultimately creates the code to call and consume this service for us, including critical details such as running the requests on background threads.
In this task, we will add a network layer to our MarsPhotos project that our ViewModel
will use to communicate with the web service. We will implement the Retrofit service API, with the following steps.
- Create a network layer, the
MarsApiService
class. - Create a Retrofit object with the base URL and the converter factory.
- Create an interface that explains how Retrofit talks to our web server.
- Create a Retrofit service and expose the instance to the api service to the rest of the app.
Implement the above steps:
- Create a new package called network. In the Android project pane, right-click on the package,
com.example.android.marsphotos
. Select New > Package. In the popup, append network to the end of the suggested package name. - Create a new Kotlin file under the new package network. Name it
MarsApiService.
- Open
network/MarsApiService.kt
. Add the following constant for the base URL for the web service.
private const val BASE_URL =
"https://android-kotlin-fun-mars-server.appspot.com"
- Just below that constant, add a Retrofit builder to build and create a Retrofit object.
private val retrofit = Retrofit.Builder()
Import retrofit2.Retrofit
, when prompted.
- Retrofit needs the base URI for the web service, and a converter factory to build a web services API. The converter tells Retrofit what to do with the data it gets back from the web service. In this case, we want Retrofit to fetch a JSON response from the web service, and return it as a
String
. Retrofit has aScalarsConverter
that supports strings and other primitive types, so we calladdConverterFactory()
on the builder with an instance ofScalarsConverterFactory
.
private val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
Import retrofit2.converter.scalars.ScalarsConverterFactory
when prompted.
- Add the base URI for the web service using
baseUrl()
method. Finally, callbuild()
to create the Retrofit object.
private val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(BASE_URL)
.build()
- Below the call to the Retrofit builder, define an interface called
MarsApiService
, that defines how Retrofit talks to the web server using HTTP requests.
interface MarsApiService {
}
- Inside the
MarsApiService
interface, add a function calledgetPhotos()
to get the response string from the web service.
interface MarsApiService {
fun getPhotos()
}
- Use the
@GET
annotation to tell Retrofit that this is a GET request, and specify an endpoint, for that web service method. In this case, the endpoint is calledphotos
. As mentioned in the previous task, we will be using /photos endpoint in this codelab.
interface MarsApiService {
@GET("photos")
fun getPhotos()
}
Import retrofit2.http.GET
when requested.
- When the
getPhotos()
method is invoked, Retrofit appends the endpointphotos
to the base URL (which we defined in the Retrofit builder) used to start the request. Add a return type of the function toString
.
interface MarsApiService {
@GET("photos")
fun getPhotos(): String
}
That is all for Day91 ✅
Thanks for reading, See you tomorrow!