Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Implement the Moshi Parser

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

After using the Gson parser manually, learn how to use the Moshi parser to automatically parse the request and response bodies.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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
)

Reviews

Comments