I have a problem with serializing data on response to an api call.
It's my first time using this.
Service is called with "Retrofit" and everything goes well, the logs report the request and the response exactly.
Al network is into NetworModule
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
@Singleton
fun provideHttpClient(): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
)
.addInterceptor { chain ->
val httpUrl: HttpUrl = chain.request().url.newBuilder().build()
val request: Request = chain.request().newBuilder().url(httpUrl).build()
chain.proceed(request)
}
.build()
@Provides
@Singleton
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.API_BASE_URL)
.client(okHttpClient)
.build()
@Provides
fun provideApiNetwork(retrofit: Retrofit): RetrofitApiNetwork =
retrofit.create(RetrofitApiNetwork::class.java)
}
response in json is:
{
"response": {
"info": {
...
},
"body": {
...
}
}
}
the problem comes in constructing a generic model with generic type. Interface of calls:
interface RetrofitApiNetwork {
@GET("....")
suspend fun getElements(): APINetworkResponse<Element>
}
Generic models is:
@Serializable
data class APINetworkResponse<T>(
val response: Response<T>
)
@Serializable
data class Response<T>(
val info: Info,
val body: T
)
The value of APINetworkResponse<Element>
is APINetworkResponse(response=null)
Can someone help me understand why and give me the solution. Thanks
Update add repository:
class DataRepository @Inject constructor(
private val retrofitApiNetwork: RetrofitApiNetwork
){
suspend fun getArtistChart(): Flow<Element> = flow {
val apiNetworkResponse: APINetworkResponse<Element> = retrofitApiNetwork.getElements()
println(apiNetworkResponse) /*APINetworkResponse(response=null)*/
}
}
add dependecies:
[versions]
androidGradlePlugin = "7.3.1"
kotlin = "1.7.10"
kotlinDsl = "2.3.3"
junit = "4.13.2"
junitExt = "1.1.4"
testCore = "1.5.0"
espresso = "3.5.0"
coreKtx = "1.9.0"
appcompat = "1.5.1"
material = "1.7.0"
retrofit = "2.9.0"
hilt = "2.44.2"
nav = "2.5.3"
[libraries]
android-pluginGradle = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
kotlin-pluginGradle = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitExt" }
espresso = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso" }
androidx-test-core = { group = "androidx.test", name = "core", version.ref = "testCore" }
androidx-test-core-ktx = { group = "androidx.test", name = "core-ktx", version.ref = "testCore" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
android-material = { group = "com.google.android.material", name = "material", version.ref = "material" }
android-retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
android-retrofit-converter = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofit" }
android-navigation = { group = "androidx.navigation", name = "navigation-fragment", version.ref = "nav" }
android-navigation-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "nav" }
android-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "nav" }
android-hilt = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
android-hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" }
[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-dsl = { id = "org.gradle.kotlin.kotlin-dsl", version.ref = "kotlinDsl" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
```
None of the answers were successful. I fixed it by adding rules to the application proguard-rules file.
I followed this documentation.