Leave a rating/review
Notes: 17. Challenge: Work with Moshi Parser
The student materials have been reviewed and are updated as of July 2022.
In this challenge, you’ll finish off learning how to work with Moshi, by implementing the last request that’s using GSON, using Moshi. You’ll also remove Gson, and the need for manual parsing, to rely on Retrofit entirely! :] That’s it! Now pause the video, and solve the challenge! Then once you’re done, unpause the video, and compare the two solutions!
To start off, open build.gradle. Remove the following dependency, as you no longer need GSON!
implementation 'com.google.code.gson:gson:2.8.6'
You’ll sync the project later, as you still have to remove the usage from the project.
Open the UserProfileResponse, and add the following annotations, to let Moshi know how it should parse the UserProfileResponse:
class UserProfileResponse(
@field:Json(name = "email") val email: String?,
@field:Json(name = "name") val name: String?)
Finally, head over to the RemoteApiService.kt, and change the types from Response and Request bodies, to concrete types:
fun getMyProfile(@Header("Authorization") token: String): Call<UserProfileResponse>
Now go to the RemoteApi.kt, and remove the Gson property, and its usage!
Well done! Now run the project, and check that everything works! :]
Good job! You’ve reached the end of this part of the course. See you in the last episode, where you will revise all the concepts you’ve learned in this part!