Network Requests with Retrofit in Android

Jun 5 2024 · Kotlin 1.9.22, Android 14, Android Studio Hedgehog | 2023.1.1

Lesson 04: Parse JSON with Moshi

Demo 1

Episode complete

Play next episode

Next
Transcript

To start, open the app module build.gradle file and add the following dependency:

implementation "com.squareup.moshi:moshi:1.15.1"
ksp "com.squareup.moshi:moshi-kotlin-codegen:1.15.1"

This adds the Moshi library to the project, together with its support for code generation. KSP stands for Kotlin Symbol Processing. It’s an API for creating compiler plugins and is a faster alternative to KAPT—Kotlin annotation processor.

Next, open the project level build.gradle file and add the following code to the plugins block:

id "com.google.devtools.ksp" version "1.8.10-1.0.9" apply false

This line includes the KSP plugin in the project, but you still need to apply it to the app module. Return to the app module build.gradle file and add this line in the plugins block at the top:

id 'com.google.devtools.ksp'

Sync the project, and you’re all set to start using Moshi.

Navigate to the model package and create a request package inside. In the request package, create a file called RegisterBody.kt. Inside that, add the following code:

@JsonClass(generateAdapter = true)
data class RegisterBody(
  val username: String,
  val password: String,
  val email: String? = null,
)

This creates a simple data-holder class for the data you send when registering a user. The annotation lets Moshi know you want it to generate an adapter that knows how to serialize RegisterBody into JSON and deserialize JSON into an instance of RegisterBody.

To put it into action, open MovieDiaryApi.kt and add this code:

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

This creates a new Moshi instance, using the Builder pattern.

Now, replace jsonBody creation in registerUser with the following code:

val registerBodyAdapter = moshi.adapter(RegisterBody::class.java)
val registerBody = RegisterBody(username, password, email)
val jsonBody = registerBodyAdapter.toJson(registerBody)

First, you obtain an instance of the adapter that Moshi generated for your RegisterBody. Then, you create an instance of RegisterBody and convert it to JSON by invoking toJson on the adapter instance — passing in the registerBody object.

Similarly, you can parse JSON string into an object using the adapter that Moshi generated, like so:

val parsedBody = registerBodyAdapter.fromJson(jsonBody)

Simply invoke fromJson and pass in a string, and Moshi tries to parse it into an object. It might throw an error or return null if something goes wrong in the process.

You can print jsonBody and parsedBody to see the results. Add the following:

println(jsonBody)
println(parsedBody)

Build and run the app. Open the Logcat and type System.out in the filtering field to clear the clutter. Try to register a user. You should see the JSON string and the RegisterBody instance printed. This confirms that the parser works correctly.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 1 Next: Instruction 2