Android Networking: Fundamentals

Sep 6 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk | 2021.2.1 Patch 1

Part 2: Implement Retrofit Basics

16. Implement the Moshi Parser

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 15. Add Queries to Calls Next episode: 17. Challenge: Work with Moshi Parser

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 16. Implement the Moshi Parser

The student materials have been reviewed and are updated as of July 2022.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Once again, as you’re going through Retrofit API calls, you saw how the part for parsing data keeps repeating, as well as some other pieces of code.

implementation "com.squareup.retrofit2:converter-moshi:2.7.1"
.addConverterFactory(MoshiConverterFactory.create().asLenient())
fun registerUser(@Body request: UserDataRequest): Call<RegisterResponse>

fun loginUser(@Body request: UserDataRequest): Call<LoginResponse>

fun getNotes(@Header("Authorization") token: String): Call<GetTasksResponse>

fun completeTask(
  @Header("Authorization") token: String,
  @Query("id") noteId: String): Call<CompleteNoteResponse>
      
fun addTask(
  @Header("Authorization") token: String,
  @Body request: AddTaskRequest): Call<Task>
class AddTaskRequest(
    @field:Json(name = "title") val title: String,
    @field:Json(name = "content") val content: String,
    @field:Json(name = "taskPriority") val taskPriority: Int)
data class UserDataRequest(
    @field:Json(name = "email") val email: String,
    @field:Json(name = "password") val password: String,
    @field:Json(name = "name") val name: String? = null
)
class CompleteNoteResponse(@field:Json(name = "message") val message: String?)
data class GetTasksResponse(@field:Json(name = "notes") val notes: List<Task> = listOf())
data class LoginResponse(@field:Json(name = "token") val token: String? = "")
data class RegisterResponse(@field:Json(name = "message")val message: String? = "")
class Task(
    @field:Json(name = "id") val id: String,
    @field:Json(name = "title") val title: String,
    @field:Json(name = "content") val content: String,
    @field:Json(name = "isCompleted") val isCompleted: Boolean,
    @field:Json(name = "taskPriority") val taskPriority: Int
)