我正在与一个我无法更改的API进行通信,该请求在API端未经过验证时发送400响应。这是一个有效的HTTP请求,但请求数据不会传递应用程序的验证规则。在Android中获取400 HTTP响应的响应正文?
400响应包含一个JSON负载,其中包含有关为什么请求未通过验证的信息。
我似乎无法得到响应正文,因为抛出了HttpRequestException。有谁知道如何检索这个响应体?
try {
HttpUriRequest request = params[0];
HttpResponse serverResponse = mClient.execute(request);
BasicResponseHandler handler = new BasicResponseHandler();
String response = handler.handleResponse(serverResponse);
return response;
} catch(HttpResponseException e) {
// Threw HttpError
Log.d(TAG, "HttpResponseException : " + e.getMessage());
Log.d(TAG, "Status Code : " + e.getStatusCode());
// TODO API returns 400 on successful HTTP payload, but invalid user data
if(e.getStatusCode() == 400) {
// Information on API error inside Response body
}
}
请发送一些代码 –