I have to handle a dynamic JSON responses.
Before, I was using classes and annotations as follows:
public class ChatResponse {
@SerializedName("status")
private int status;
@SerializedName("error")
private String error;
@SerializedName("response")
private Talk response;
public int getStatus() {
return status;
}
public String getError() {
return error;
}
public Talk getResponse() {
return response;
}
}
When the status is 1 (success) the onResponse
is fired and I can get a ChatResponse object. But, when the status is 0, the response is false in the JSON representation and it fails (onFailure
is fired).
I want to create my custom converter, and this question has a good example, but that example is for Retrofit 1.
I have to create a class that extends Converter.Factory
, but I don't know how to override the methods of this class.
Actually I have the next:
@Override
public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
return super.fromResponseBody(type, annotations);
}
@Override
public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
return super.toRequestBody(type, annotations);
}
How I can parse the JSON response by my own at this point?
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…