Android Moshi日期适配器begin\u对象,但为begin\u数组
我正在项目中使用Android Moshi日期适配器begin\u对象,但为begin\u数组,android,kotlin,retrofit2,moshi,Android,Kotlin,Retrofit2,Moshi,我正在项目中使用改装和moshi库来帮助我连接到后端。从那以后,我把日期发了回去,但显然,moshi无法处理日期。我已经编写了自己的JsonAdapter,但现在我发现了错误: com.squareup.moshi.JsonDataException:应为BEGIN_对象,但在路径处为BEGIN_数组$ 日期适配器: class-DateAdapter:JsonAdapter(){ @FromJson 覆盖来自JSON的乐趣(读卡器:JsonReader):日期{ val value=reade
改装
和moshi
库来帮助我连接到后端。从那以后,我把日期发了回去,但显然,moshi
无法处理日期。我已经编写了自己的JsonAdapter,但现在我发现了错误:
com.squareup.moshi.JsonDataException:应为BEGIN_对象,但在路径处为BEGIN_数组$
日期适配器:
class-DateAdapter:JsonAdapter(){
@FromJson
覆盖来自JSON的乐趣(读卡器:JsonReader):日期{
val value=reader.nextString()
返回SimpleDataFormat(“yyyy-MM-dd”,Locale.FRENCH).parse(值)
//返回getDateInstance(DateFormat.LONG,Locale.FRENCH).parse(值)
}
@ToJson
重写JSON(writer:JsonWriter,value:Date?){
writer.value(simpleDataFormat(“yyyy-MM-dd”,Locale.FRENCH).format(value))
}
}
网络层
我做了一些研究,大多数人都在谈论这样一个事实:你得到的是一个数组而不是一个单一的对象,有些东西需要改变,但我不知道要做什么或添加什么,你的json是从数组开始的,所以你必须在数组中像这样在接口中设置改装响应(这是JAVA中的ans):-
调用getHomeContent()代码>可能存在的重复项
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(DateAdapter())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()
// the request that throws the error
@GET("getPartiesNearYou")
fun getPatiesNearYou(
@Query("distance") distance: Int,
@Query("lat") lat: Double,
@Query("long") long: Double,
@Query("userId") userId: String
): Deferred<NetworkPartyContainer>
[
{
"location": {
"type": "Point",
"coordinates": [
50,
50
]
},
"participants": [
"5db76b7430957f0ef05e73fa"
],
"declines": [
null,
"5dc322e02c7171369e4c67fb"
],
"_id": "5dc322712c7171369e4c67fa",
"name": "Mout's Hartenjagen Party",
"date": "2019-11-28T23:00:00.000Z",
"maxSize": 4,
"gameId": "5db76b7430957f0ef05e73fa",
"createdAt": "2019-11-06T19:43:45.544Z",
"updatedAt": "2019-11-06T19:49:07.599Z",
"__v": 0
}
]