edwardwongtl
04/24/2018, 9:29 AMdata class Response<T>(
...
val data: List<T>
}
Has anyone using moshi to deal with this kind of object? I don't quite seems to find example for doing so..nikolay
04/24/2018, 10:30 AMedwardwongtl
04/24/2018, 10:47 AMjava.lang.ClassCastException: com.squareup.moshi.LinkedHashTreeMap cannot be cast to T
nikolay
04/24/2018, 11:02 AMedwardwongtl
04/24/2018, 11:02 AMnikolay
04/24/2018, 11:03 AMedwardwongtl
04/24/2018, 11:06 AMnikolay
04/24/2018, 12:34 PMnikolay
04/24/2018, 12:54 PMeric
04/24/2018, 3:44 PMedwardwongtl
04/24/2018, 3:46 PMfromJson
part, cause there are other properties other then val data: List<T>
.edwardwongtl
04/24/2018, 3:48 PMobject CustomMoshiAdapterFactory: JsonAdapter.Factory {
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
val rawType = Types.getRawType(type)
if (rawType == PageResponse::class.java && type is ParameterizedType) {
val subType = type.actualTypeArguments.first()
val adapter: JsonAdapter<PageResponse<*>> = moshi.adapter(Types.newParameterizedType(rawType, subType))
return adapter
}
return null
}
}
eric
04/24/2018, 3:52 PMedwardwongtl
04/24/2018, 3:55 PMval adapter...
to val adapter: JsonAdapter<Any> = moshi.adapter(subType)
?nikolay
04/24/2018, 4:14 PMedwardwongtl
04/24/2018, 4:14 PMedwardwongtl
04/24/2018, 4:17 PMeric
04/24/2018, 4:18 PMedwardwongtl
04/24/2018, 4:20 PMdata class Response<T> ( ..., val data: List<T>)
thing properly, moshi gives me java.lang.ClassCastException: com.squareup.moshi.LinkedHashTreeMap cannot be cast to T
for whatever T
I putedwardwongtl
04/24/2018, 4:23 PMeric
04/24/2018, 4:31 PMedwardwongtl
04/24/2018, 4:35 PMval movieResponseList: MutableList<MovieResponse> = mutableListOf()
override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
val movie = movieResponseList[position].movie
holder.bindData(movie)
// set on click listener and delegate out
if (onItemClickListener != null) {
holder.itemView.setOnClickListener { it ->
onItemClickListener!!.onItemClick(it, position)
}
}
}
The stacktrace said it is the val movie = movieResponseList[position].movie
gives such exception.
Remark: The server api is returning Response<MovieResponse>
in this case, where data class MovieResponse(val movie: Movie)
and Movie
is also some simple data class.nikolay
04/24/2018, 4:39 PMedwardwongtl
04/24/2018, 4:41 PMeric
04/24/2018, 4:41 PMnikolay
04/24/2018, 4:43 PMnikolay
04/24/2018, 4:44 PMnikolay
04/24/2018, 4:44 PMedwardwongtl
04/24/2018, 4:44 PMnikolay
04/24/2018, 6:07 PMedwardwongtl
04/25/2018, 4:53 AM{
"content": [
{
"organisation": null,
"movie": {
"name": "Iron man",
"posterUrl": "someUrl",
"durationMin": 130,
"openDate": "0999-01-01T00:00:00Z",
"status": "ACTIVE",
"publish": "RELEASE",
"sort": 1000,
"uuid": "e56e36d4-ab32-4a9a-8312-817860719847",
"createAt": "2018-04-24T04:03:45Z",
"updateAt": "2018-04-24T04:03:45Z"
},
"count": null
},
{
"organisation": null,
"movie": {
"name": "Thor",
"posterUrl": "someOtherUrl",
"durationMin": 121,
"openDate": "0999-01-01T00:00:00Z",
"status": "ACTIVE",
"publish": "RELEASE",
"sort": 1000,
"uuid": "ca74e40c-ef8b-4730-a2d2-4b87f6ac49f9",
"createAt": "2018-04-24T04:03:45Z",
"updateAt": "2018-04-24T04:03:45Z"
},
"count": null
}
],
"last": true,
"totalPages": 1,
"totalElements": 12,
"sort": null,
"first": true,
"numberOfElements": 12,
"size": 0,
"number": 0
}
Damn it is just weird, when using moshi.adapter(Types.newParameterizedType(PageResponse::class.java, MovieResponse::class.java))
, it does actually parse without issue.
But when it combined with Retrofit
, RxJavaCallAdapter
and MoshiConverter
, the issue happens in my project.nikolay
04/25/2018, 7:44 AMdata class Response(
val content: List<ContentItem>,
val last: Boolean,
val totalPages: Int,
val totalElements: Int,
val sort: Boolean?
) {
data class ContentItem(
val organization: String?,
val movie: Movie,
val count: Int?
) {
data class Movie(
val name:String,
val poeterUrl: String
)
}
}
nikolay
04/25/2018, 7:44 AMedwardwongtl
04/25/2018, 7:45 AMMovieResponse
is not the only structure that can be inside Response<T>
edwardwongtl
04/25/2018, 7:46 AMResponse<Foo>
, Response<Bar>
, Response<Whatever>
etc.edwardwongtl
04/25/2018, 7:50 AMConductor
).
Sorry for wasting so much time of you two gentleman.nikolay
04/25/2018, 8:47 AMResponse<Foo>
and so on, does it mean that one endpoint return different responses ?edwardwongtl
04/25/2018, 8:48 AMFooResponse
, BarResponse
so many times when I can express it using generics, and that's the purpose of generics.