【发布时间】:2017-07-26 13:56:07
【问题描述】:
我正在为改造而苦苦挣扎。当我在浏览器中发布请求时,我收到了这样的请求:
这就是我所期望的。但是,当我尝试在我的应用程序中解析它时,我不断收到回复,就像在this 线程中一样。我发现尝试实施this 解决方案,但我的errorBody 甚至与我浏览器中的答案都不相似:
我怎样才能得到这个 JSON?
以防万一这是我的响应处理程序代码:
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;
}
}
【问题讨论】:
-
response.errorBody().toString(),是 JSON 吗? -
没有。至少我不认识它是:“okhttp3.ResponseBody$1@f0473b9”
-
您正在尝试从 GSON 的 fromJson 构造
errorResponse,它需要 JSON。你知道你哪里做错了吗? -
啊。这就是你的意思。我看到我使用了
toString()而不是string()。当我评估response.errorBody().string()我得到{"error":{"message":"Not Found","error":{"status":404}}}这就是我想要的。我将发布您的答案以将问题标记为已解决。谢谢:) -
很高兴能帮上忙!!
标签: android json retrofit2 gson