[英]Get response body on 400 HTTP response in Android?
I'm communicating with an API that I can not change that sends a 400 response when a request is not validated on the API side.我正在与一个我无法更改的 API 通信,当 API 端未验证请求时发送 400 响应。 It is a valid HTTP request, but the request data does not pass the application's validation rules.
这是一个有效的 HTTP 请求,但请求数据没有通过应用程序的验证规则。
The 400 response contains a JSON payload that has information on why the request did not pass validation. 400 响应包含一个 JSON 有效负载,其中包含有关请求未通过验证的原因的信息。
I can't seem to get the response body because an HttpRequestException is thrown.我似乎无法获得响应正文,因为抛出了 HttpRequestException。 Does anybody know how to retrieve this response body?
有人知道如何检索此响应正文吗?
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
}
}
Something like this, using org.apache.http.util.EntityUtils
:像这样,使用
org.apache.http.util.EntityUtils
:
HttpRequestBase base = new HttpGet(url);
HttpResponse response = httpClient.execute(base);
String jsonString = EntityUtils.toString(response.getEntity());
PS: This is blind coding as I don't know what you tried yet. PS:这是盲编码,因为我不知道你尝试了什么。
To get status code:获取状态码:
int statusCode = response.getStatusLine().getStatusCode();
To get the entity body in byte array:要获取字节数组中的实体主体:
byte[] data = EntityUtils.toByteArray(response.getEntity());
Try that way:试试这样:
if (this.responseCode == HttpURLConnection.HTTP_OK) {
inputStream = httpUrlConnection.getInputStream();
} else {
inputStream = httpUrlConnection.getErrorStream();
}
if (serverResponseCode == 200) {
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
for (String line; (line = r.readLine()) != null; ) {
total.append(line).append('\n');
}
Log.e("temp", String.valueOf(total));
}
else {
InputStream in = new BufferedInputStream(conn.getErrorStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
for (String line; (line = r.readLine()) != null; ) {
total.append(line).append('\n');
}
Log.e("temp", String.valueOf(total));
}
This is how I send and get Http response as an byte[]
.Of course you can change it to string if you want.这就是我以
byte[]
形式发送和获取 Http 响应的方式。当然,如果需要,您可以将其更改为字符串。
byte[] buffer = new byte[1024];
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.rpc.booom.com");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("debug_data","1"));
postParameters.add(new BasicNameValuePair("client_api_ver", "1.0.0.0"));
postParameters.add(new BasicNameValuePair("device_identificator", deviceId));
postParameters.add(new BasicNameValuePair("device_resolution", resolution));
postParameters.add(new BasicNameValuePair("username_hash", hashUser(username,password)));
postParameters.add(new BasicNameValuePair("password_hash", hashPass(username,password)));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
Log.w("Response ","Status line : "+ response.getStatusLine().toString());
buffer = EntityUtils.toString(response.getEntity()).getBytes();
Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.