从改造的响应错误获取json
我正在努力进行改型.当我在浏览器中发布请求时,我收到这样的请求:
I am struggling with retrofit. When I post a request in my browser i get such a request:
这就是我的期望.但是,当我尝试在应用程序中解析此内容时,我一直收到此解决方案,但是我的errorBody
甚至与我的浏览器的答案都不相似:
如何获取此JSON?
And that's what I expect. However, when I try to parse this in my app I kept getting responses as in this thread. I've found tried to implement this solution, but my errorBody
does not even resemble the answer from my browser:
How can I get this JSON?
以防这是我的响应处理程序代码:
Just in case this is my response handler code:
void handleResponse(Response response){
TextView textView = (TextView)findViewById(R.id.empty_list_tv);
if(response.isSuccessful())
textView.setText(response.toString());
else {
Gson gson = new Gson();
ErrorResponse errorResponse = gson.fromJson(
response.errorBody().toString(),
ErrorResponse.class);
textView.setText(response.errorBody().toString());
}
}
还有我的ErrorResponse
:
public class ErrorResponse {
@SerializedName("message")
private String message;
@SerializedName("error")
private Error error;
public String getMessage() {
return message;
}
public Error getError() {
return error;
}
}
您正在GSON的fromJson中使用toString()
,这不是JSON内容.将toString()
替换为string()
,这将为您提供JSON正文.
另外,请确保只使用string()
方法一次并将响应保存在变量中,因为如果再次使用它会返回空字符串.
You are using toString()
in GSON's fromJson which is not a JSON content. Replace your toString()
as string()
which will give you the JSON body.
Also make sure to use the string()
method only once and save the response in a variable, because it will return empty string if you used it again.