我需要处理动态JSON响应。
以前,我使用以下类和注释:
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;
}
}
当状态为1(成功)时,将触发
onResponse
,我可以获取ChatResponse对象。但是,当状态为0时,在JSON表示中响应为false,因此失败(触发onFailure
)。我想创建自定义转换器,这个问题有一个很好的例子,但是该示例适用于Retrofit 1。 我必须创建一个扩展
Converter.Factory
的类,但我不知道如何覆盖此类的方法。实际上,我有以下内容:
@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);
}
我该如何在此时解析JSON响应?提前感谢。