【发布时间】:2020-04-27 16:15:04
【问题描述】:
我正在使用 Kotlin 和 retrofit+gson 进行网络调用,但我无法反序列化嵌套 json 的响应
这是我的网络模块
类 NetworkModule() {
@Provides
internal fun provideGson(): Gson {
return GsonBuilder()
.setLenient()
.disableHtmlEscaping()
.create()
}
@Provides
internal fun provideOkHttpClient(): OkHttpClient {
val builder = OkHttpClient.Builder()
builder.readTimeout(60, TimeUnit.SECONDS)
builder.connectTimeout(60, TimeUnit.SECONDS)
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.addInterceptor(logging)
.build()
return client
}
@Provides
internal fun provideLuqyaApi(gson: Gson, okHttpClient: OkHttpClient): LuqyaApi {
return Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL_PLUS)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build().create(LuqyaApi::class.java)
}
这是模型
data class HomeResponse(
@field:SerializedName("code")
@Expose
val code: String? = null,
@field:SerializedName("data")
@Expose
val homeDataItem: HomeDataItem,
@field:SerializedName("message")
@Expose
val message: String? = null,
@field:SerializedName("notifications")
@Expose
val notifications: Int? = null
)
class HomeDataItem {
@SerializedName("specialEvent")
val specialEvent: SpecialEvent?=null
@SerializedName("homeAdvertisment")
val homeAdvertisment: HomeAdvertisment?=null
@SerializedName("locations")
val locations=ArrayList<LocationsItem>()
}
我收到这样的回复
HomeResponse(code=201, homeDataItem=m.a.b.b.a.b.b@ada2b35, message=所有家庭位置和一个广告,notifications=16)
这里有什么问题?
【问题讨论】:
标签: android kotlin gson retrofit2