Networking Codelab Flashcards
Which 2 dependencies need to be added to the module’s build.gradle to implement Retrofit?
What is the purpose of both dependencies?
“com.squareup.retrofit2:retrofit:$version_retrofit”
“com.squareup.retrofit2:converter-scalars:$version_retrofit”
retrofit2:retrofit library:
1 - establishes network connection to a server
2 - communicates with the server
3 - receives and parses the JSON response into a useable format
4 - handles calls on background threads
converter-scalars: enables Retrofit to return the JSON result as a String.
How do you add support for libraries looking to take advantage of Java 8 language features?
In build.gradle (:app)
android {
…
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
What is required to build a Retrofit instance?
private val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
OR
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
At a minimum, Retrofit requires a base url and a converter factory in order to build a web services API.
What does the base url point to?
What does an appended url point to?
Base url points to the backend server.
Appended url points to and endpoint.
What else can a JSON object sometimes be called?
dictionary / hash map / associative array
- What does REST stand for?
- What does SOAP stand for?
- What is the difference between these two?
- Representational State Transfer
- Simple Object Access Protocol
- REST - JSON or XML / HTTP - more flexible than SOAP
SOAP - XML / usually HTTP but can be SMTP (Simple Mail Transfer Protocol)
Which error may require you to uninstall and re run the app after setting up the first network call?
java.net.SocketException: socket failed: EPERM (Operation not permitted)
EPERM and EACCES are errors for processes that require root/super user
What does API stand for?
Application Programming Interface
What are the four HTTP methods for API calls?
GET (read)
POST (create)
PUT (update)
DELETE (delete)
CRUD = create, read, update, delete
Define an interface for an API call
interface MyApiService{
@CRUD_METHOD(“endpoint”)
suspend fun myCrudFunction( ): ReturnType
}
Without coroutines, ReturnType is Call; the Call object is used to start the request.
When implementing coroutines, the call is launched from the coroutine and the ReturnType will likely be List.
suspend fun when using coroutines instead of Callbacks
Define an implementation of an API interface
object MyApi {
val retrofitService : MyApiService by lazy {
retrofit.create(MyApiService::class.java) }
}
object - so each call to MyApi.retrofitService returns a singleton - only one is required in the app
lazy - because the call is computationally expensive
Using Callbacks, not coroutines, make an API call using the implementation of your Api interface
MyApi.retrofitService.myCrudFunction( ).enqueue( object: Callback { // implement onFailure( ) and onResponse( ) } )
enqueue starts the request on a background thread
ExpectedReturnType will likely be List unless no conversion to objects has been implemented, in which case probably String
With Callbacks (not coroutines), how does one handle the API response?
Use a LiveData
onFailure(call: Call, t: Throwable) {
liveData.value = “Failure: “ + t.message
}
onResponse(call: Call, response: Response) {
liveData.value = response.body( )
}
What needs adding to the Manifest before implementing API calls?
^uses-permission android:name=”android.permission.INTERNET” /^
above the ^application^ tag
To implement Moshi, what needs to be done with dependencies?
In the module’s build.gradle:
Add
implementation “com.squareup.moshi:moshi-kotlin:$version_moshi”
Replace converter-scalars implementation with
implementation “com.squareup.retrofit2:converter-moshi:$version_retrofit”