Day94 of #100DaysOfCode

Day94 of #100DaysOfCode

Unit 4: Internet

Add Moshi library dependencies

// Moshi
implementation 'com.squareup.moshi:moshi-kotlin:1.13.0'
// Retrofit with Moshi Converter
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
[{
"id":"424906",
"img_src":"http://mars.jpl.nasa.gov/msl-raw-images/msss/01000/mcam/1000ML0044631300305227E03_DXXX.jpg"
},
...]

Moshi parses this JSON data and converts it into Kotlin objects. To do this, Moshi needs to have a Kotlin data class to store the parsed results, so in this step we will create the data class, MarsPhoto.

data class MarsPhoto(
)
data class MarsPhoto(
val id: String, val img_src: String
)
@Json(name = "img_src") val imgSrcUrl: String
import retrofit2.converter.scalars.ScalarsConverterFactory
private val moshi = Moshi.Builder()
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
interface MarsApiService {
@GET("photos")
suspend fun getPhotos(): List<MarsPhoto>
}
_status.value = "Success: ${listResult.size} Mars photos retrieved"

That is all for Day94 ✅

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store