Android Networking: Fundamentals

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

Part 2: Implement Retrofit Basics

17. Challenge: Work with 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: 16. Implement the Moshi Parser Next episode: 18. Conclusion

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: 17. Challenge: Work with Moshi Parser

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

Transcript: 17. Challenge: Work with Moshi Parser

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!