我与我不能改变发送400响应当请求未在API端验证的API进行通信。它是一个有效的HTTP请求,但请求的数据不通过应用程序的验证规则。
400响应包含对为什么请求没有通过验证信息的JSON有效载荷。
我似乎不能,因为Htt的prequestException被抛出,以获得响应主体。有谁知道如何找回这种反应的身体吗?
{尝试 HttpUriRequest请求=参数[0]; HTT presponse serverResponse = mClient.execute(请求); BasicResponseHandler处理程序=新BasicResponseHandler(); 字符串的响应= handler.handleResponse(serverResponse); 返回响应; }赶上(HTT presponseException E){ //投掷了HTTPError这样的 Log.d(TAG的Htt presponseException:+ e.getMessage()); Log.d(TAG状态code:+ e.getStatus code()); // TODO API成功的HTTP负载返回400,但用户数据无效 如果(e.getStatus code()== 400){ //对API错误响应体内信息 } }
解决方案
这是我如何发送和获取HTTP响应为字节[]
。当然,你可以改变它的字符串,如果你想要的。
字节[]缓冲区=新的字节[1024]; HttpClient的=新DefaultHttpClient(); httppost =新HttpPost(http://www.rpc.booom.com); postParameters =新的ArrayList<&的NameValuePair GT;(); postParameters.add(新BasicNameValuePair(debug_data,1)); postParameters.add(新BasicNameValuePair(client_api_ver,1.0.0.0)); postParameters.add(新BasicNameValuePair(device_identificator,DEVICEID)); postParameters.add(新BasicNameValuePair(device_resolution)号决议); postParameters.add(新BasicNameValuePair(username_hash,hashUser(用户名,密码))); postParameters.add(新BasicNameValuePair(password_hash,hashPass(用户名,密码))); httppost.setEntity(新UrlEn codedFormEntity(postParameters)); HTT presponse响应= httpclient.execute(httppost); Log.w(响应,状态行:+ response.getStatusLine()的toString()); 缓冲液= EntityUtils.toString(response.getEntity())的getBytes();
希望它帮助!
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. It is a valid HTTP request, but the request data does not pass the application's validation rules.
The 400 response contains a JSON payload that has information on why the request did not pass validation.
I can't seem to get the response body because an HttpRequestException is thrown. 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
}
}
解决方案
This is how I send and get Http response as an byte[]
.Of course you can change it to string if you want.
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!