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

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

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"
id "com.google.devtools.ksp" version "1.8.10-1.0.9" apply false
id 'com.google.devtools.ksp'
@JsonClass(generateAdapter = true)
data class RegisterBody(
  val username: String,
  val password: String,
  val email: String? = null,
)
private val moshi = Moshi.Builder().build()
val registerBodyAdapter = moshi.adapter(RegisterBody::class.java)
val registerBody = RegisterBody(username, password, email)
val jsonBody = registerBodyAdapter.toJson(registerBody)
val parsedBody = registerBodyAdapter.fromJson(jsonBody)
println(jsonBody)
println(parsedBody)
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 1 Next: Instruction 2