admin管理员组文章数量:1310034
I am building an app need to pull some data from 2 endpoints: /api/products
and /api/type
The first endpoint return a JSON and this is working fine. However, the second endpoint does not return a json but an Array of object.
The response sent back by api/type
looks like this :
[["Model",123],["ModelB",456],["ModelC",789]]
when the other endpoints return the "usual" json.
I am using retrofit, okhttp and dagger-hilt.
The retrofit module is setup as below:
@InstallIn(SingletonComponent::class)
@Module
class APIModule {
@Singleton
@Provides
@Named("default")
fun provideDefaultOkHttpClient(): OkHttpClient =
OkHttpClient
.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build()
@Singleton
@Provides
fun provideRetrofit(
@Named("default") okHttpClient: OkHttpClient
): Retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(";)
.client(okHttpClient)
.build()
}
This is working fine when calling api/products
but I got an error below when using api/type
:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
The Api service is as below:
interface Service {
@GET("api/type")
suspend fun getType(): Array<Pair<String, Int>>
I understand that the issue is that for one endpoint the response is a JSON and in the other case, it's an array like: [["Model",123],["ModelB",456],["ModelC",789]]
Any idea how to make it works ?
Also is there any addConverterFactory
that allow me to support both json and array at the same time ?
I am building an app need to pull some data from 2 endpoints: /api/products
and /api/type
The first endpoint return a JSON and this is working fine. However, the second endpoint does not return a json but an Array of object.
The response sent back by api/type
looks like this :
[["Model",123],["ModelB",456],["ModelC",789]]
when the other endpoints return the "usual" json.
I am using retrofit, okhttp and dagger-hilt.
The retrofit module is setup as below:
@InstallIn(SingletonComponent::class)
@Module
class APIModule {
@Singleton
@Provides
@Named("default")
fun provideDefaultOkHttpClient(): OkHttpClient =
OkHttpClient
.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build()
@Singleton
@Provides
fun provideRetrofit(
@Named("default") okHttpClient: OkHttpClient
): Retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://test")
.client(okHttpClient)
.build()
}
This is working fine when calling api/products
but I got an error below when using api/type
:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
The Api service is as below:
interface Service {
@GET("api/type")
suspend fun getType(): Array<Pair<String, Int>>
I understand that the issue is that for one endpoint the response is a JSON and in the other case, it's an array like: [["Model",123],["ModelB",456],["ModelC",789]]
Any idea how to make it works ?
Also is there any addConverterFactory
that allow me to support both json and array at the same time ?
1 Answer
Reset to default 1Create a class to be used in the data structure:
@JsonAdapter(DataDeserializer::class)
internal data class Data(
val string: String,
val value: Int,
)
Write custom deserializer:
internal class DataDeserializer : JsonDeserializer<Data> {
override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): Data {
val jsonArray = requireNotNull(json?.asJsonArray)
val string = jsonArray[0].asString
val value = jsonArray[1].asInt
return Data(string, value)
}
}
The service will look like this:
internal interface Service {
@GET("api")
suspend fun getData(): Array<Data>
}
Be sure to specify addConverterFactory(GsonConverterFactory.create())
and you can substitute test data for the server by adding an interceptor:
@Singleton
@Provides
fun provideDefaultOkHttpClient(): OkHttpClient =
OkHttpClient
.Builder()
.addInterceptor { chain ->
Response.Builder()
.request(chain.request())
.code(200)
.protocol(Protocol.HTTP_2)
.message("message")
.body("[[\"Model\",123],[\"ModelB\",456],[\"ModelC\",789]]".toResponseBody("application/json".toMediaType()))
.build()
}
.build()
@Singleton
@Provides
fun provideRetrofit(
okHttpClient: OkHttpClient
): Retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://test")
.client(okHttpClient)
.build()
本文标签: androidHow to handle Array of data in retrofit responseStack Overflow
版权声明:本文标题:android - How to handle Array of data in retrofit response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741854091a2401237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论