jw
06/25/2018, 1:42 PMjw
06/25/2018, 1:42 PMAyden
07/18/2018, 1:02 PM{
"status": true,
"code": null,
"result": {
"id": 1,
"name": "User",
"email": "<mailto:user@gmail.com|user@gmail.com>",
"created_at": "2018-07-12 15:25:42",
"updated_at": "2018-07-12 15:25:42"
}
}
This is my data class.
data class Response (
@Json(name = "status") val status: Boolean?,
@Json(name = "code") val code: Int?,
@Json(name = "result") val result: UserResult?
)
data class UserResult (
@Json(name = "id") val id: Int?,
@Json(name = "name") val name: String?,
@Json(name = "email") val email: String?
)
This is my Retrofit call.enqueue
call.enqueue(object : Callback<Response> {
override fun onFailure(call: Call<Response>?, t: Throwable?) {
...
}
override fun onResponse(call: Call<Response>?, response: Response<Response>) {
if (response.isSuccessful) {
val result = response.body()?.result.toString()
// The result being return is id=1, name=Username, email=username@email.com according to Log.d
// How to extract the user information
}
}
})
May I know how to extract the user information from the response given?
I have try Moshi Custom Type Adapter
and various of method.
But I would either get Conversation error
or wrong result.
PS: I am double posting this on different channel because I don't know which channel is appropriate to ask thiseric
07/18/2018, 5:02 PMThe result being return is id=1, name=Username, email=username@email.com according to Log.dsounds correct. what's the question? what's a "Conversion error?"
Ayden
07/19/2018, 1:13 AMConversion error
happened when the user is empty.
But now one of the issue is I don't know how to extract the json and store it into a variable. 😞eric
07/19/2018, 4:48 PMAyden
07/20/2018, 2:11 AMAyden
07/20/2018, 2:11 AMAyden
07/20/2018, 2:11 AMcall.enqueue
show null.Ayden
07/20/2018, 2:11 AMdave08
07/25/2018, 2:58 PMJsonRpcResponse<List<User>>
?jw
07/25/2018, 2:59 PMTypes
which you can then use to look up an adapterdave08
07/25/2018, 3:00 PMTypes.newParameterizedType(JsonRpcResponse::class.java, resultType)
but even though I did the same for the list and passed it as resultType
I get Types.newParameterizedType(JsonRpcResponse::class.java, resultType)
dave08
07/25/2018, 3:00 PMval resultType = Types.newParameterizedType(List::class.java, Type::class.java)
dave08
07/25/2018, 3:01 PMdata class JsonRpcResponse<out R>(val id: Int, val result: R? = null, val error: JsonRpcError? = null, val jsonrpc:String = "2.0")
dave08
07/25/2018, 3:02 PMjw
07/25/2018, 3:02 PMdave08
07/25/2018, 3:03 PMjava.lang.IllegalArgumentException: No JsonAdapter for interface java.lang.reflect.Type (with no annotations)
@jw...jw
07/25/2018, 3:04 PMType
instead of User
for the list parameterdave08
07/25/2018, 3:06 PMJsonRpcResponse<List<User>>
, so I can't just pass User to the result parameter of JsonRpcResponse, I need it to be in a List and that gets erased also...dave08
07/25/2018, 3:07 PMval resultType = Types.newParameterizedType(List::class.java, Type::class.java)
val responseType = Types.newParameterizedType(JsonRpcResponse::class.java, resultType)
val response = moshi.adapter<JsonRpcResponse<R>>(responseType).fromJson(stringResponse)!!
jw
07/25/2018, 3:08 PMType
instead of User
as the parameter of the list that is resultType
eric
07/25/2018, 5:08 PMdave08
07/26/2018, 5:31 AMpnih
07/26/2018, 11:49 AM@Retention(AnnotationRetention.RUNTIME)
@JsonQualifier
//@Target(AnnotationTarget.FIELD)
annotation class EmptyString
internal class StringAdapter {
@FromJson @EmptyString
fun fromJson(reader: JsonReader) = when (reader.peek()) {
JsonReader.Token.BOOLEAN -> when (reader.nextBoolean()) {
false -> null // Response was false
else ->
throw IllegalStateException("Non-false boolean for @JsonObjectOrFalse field")
}
JsonReader.Token.STRING -> ""
else ->
throw IllegalStateException("Non-object-non-boolean value for @JsonObjectOrFalse field")
}
@ToJson
fun toJson(writer: JsonWriter, @EmptyString value: String?) {}
}
That this test fails with:
class JsonParserTest {
@Test
fun testEmptyStringParser(){
val moshi = Moshi.Builder().add(StringAdapter()).add(KotlinJsonAdapterFactory()).build()
val nullJson = """
{ "name": false }
""".trimIndent()
val result = moshi.adapter<TestData>(TestData::class.java).fromJson(nullJson)!!
assert(TestData("Hello") == result)
}
}
com.squareup.moshi.JsonDataException: Non-null value 'name' was null at $.name
. Am I missing something?eric
07/26/2018, 8:28 PMeric
07/26/2018, 8:28 PMeric
07/26/2018, 8:29 PMAyden
08/01/2018, 12:27 PMAyden
08/01/2018, 12:27 PM