Networking Codelab Flashcards

1
Q

Which 2 dependencies need to be added to the module’s build.gradle to implement Retrofit?

What is the purpose of both dependencies?

A

“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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you add support for libraries looking to take advantage of Java 8 language features?

A

In build.gradle (:app)
android {

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is required to build a Retrofit instance?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the base url point to?

What does an appended url point to?

A

Base url points to the backend server.

Appended url points to and endpoint.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What else can a JSON object sometimes be called?

A

dictionary / hash map / associative array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What does REST stand for?
  2. What does SOAP stand for?
  3. What is the difference between these two?
A
  1. Representational State Transfer
  2. Simple Object Access Protocol
  3. REST - JSON or XML / HTTP - more flexible than SOAP
    SOAP - XML / usually HTTP but can be SMTP (Simple Mail Transfer Protocol)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which error may require you to uninstall and re run the app after setting up the first network call?

A

java.net.SocketException: socket failed: EPERM (Operation not permitted)

EPERM and EACCES are errors for processes that require root/super user

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does API stand for?

A

Application Programming Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the four HTTP methods for API calls?

A

GET (read)
POST (create)
PUT (update)
DELETE (delete)

CRUD = create, read, update, delete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define an interface for an API call

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define an implementation of an API interface

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using Callbacks, not coroutines, make an API call using the implementation of your Api interface

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

With Callbacks (not coroutines), how does one handle the API response?

A

Use a LiveData

onFailure(call: Call, t: Throwable) {
liveData.value = “Failure: “ + t.message
}

onResponse(call: Call, response: Response) {
liveData.value = response.body( )
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What needs adding to the Manifest before implementing API calls?

A

^uses-permission android:name=”android.permission.INTERNET” /^

above the ^application^ tag

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

To implement Moshi, what needs to be done with dependencies?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does Moshi do?

Provide syntax as an example of how.

A

Moshi converts/maps a JSON into custom Kotlin objects.

data class MyObject(
@Json(name="endpoint_key") val myObjectAttribute: Type
)

NB - if the endpoint_key also makes a good attribute name, you can just declare: val endpoint_key: Type

17
Q

How do you build a Moshi instance?

A

private val moshi = Moshi.Builder( )
.add(KotlinJsonAdapterFactory( ) )
.build( )

18
Q

With coroutines (not Callbacks), how does one handle the API response?

A
viewModelScope.launch {
  try {
    val listResult = MyApi.retrofitService.myCrudFunction()
    // use LiveData and manipulate function's return value here
  } catch (e: Exception) {
  // manipulate $e here
  }
}