Android 在 HttpClient 中收到 403 后得到响应
我有这样的代码:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(server);
try {
JSONObject params = new JSONObject();
params.put("email", email);
StringEntity entity = new StringEntity(params.toString(), "UTF-8");
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(httpPost, responseHandler);
JSONObject response = new JSONObject(responseBody);
fetchUserData(response);
saveUserInfo();
return true;
} catch (ClientProtocolException e) {
Log.d("Client protocol exception", e.toString());
return false;
} catch (IOException e) {
Log.d`enter code here`("IOEXception", e.toString());
return false;
} catch (JSONException e) {
Log.d("JSON exception", e.toString());
return false;
}
即使我有 HTTP 403 Forbidden 来获取错误消息,我也希望得到响应
I have a code like this:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(server);
try {
JSONObject params = new JSONObject();
params.put("email", email);
StringEntity entity = new StringEntity(params.toString(), "UTF-8");
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(httpPost, responseHandler);
JSONObject response = new JSONObject(responseBody);
fetchUserData(response);
saveUserInfo();
return true;
} catch (ClientProtocolException e) {
Log.d("Client protocol exception", e.toString());
return false;
} catch (IOException e) {
Log.d`enter code here`("IOEXception", e.toString());
return false;
} catch (JSONException e) {
Log.d("JSON exception", e.toString());
return false;
}
And i want to have a response even if I have HTTP 403 Forbidden to get error message
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BasicResponseHandler
仅在返回成功代码 (2xx) 时才返回您的数据。但是,您可以非常轻松地编写自己的ResponseHandler
来始终以String
形式返回响应正文,例如,或者,您可以使用 其他重载执行方法 上
HttpClient
不需要ResponseHandler
并直接返回HttpResponse
。然后以相同的方式调用 EntityUtils.toString(response.getEntity()) 。要获取响应的状态代码,您可以使用
HttpResponse.getStatusLine().getStatusCode()
并与HttpStatus
类中的静态整数之一进行比较。例如,代码“403”是HttpStatus.SC_FORBIDDEN
。您可以根据返回的状态代码采取与您的应用程序相关的特定操作。The
BasicResponseHandler
only returns your data if a success code (2xx) was returned. However, you can very easily write your ownResponseHandler
to always return the body of the response as aString
, e.g.Alternatively, you can use the other overloaded execute method on
HttpClient
which does not require aResponseHandler
and returns you theHttpResponse
directly. Then callEntityUtils.toString(response.getEntity())
in the same way.To get the status code of a response, you can use
HttpResponse.getStatusLine().getStatusCode()
and compare to to one of the static ints in theHttpStatus
class. E.g. code '403' isHttpStatus.SC_FORBIDDEN
. You can take particular actions as relevant to your application depending on the status code returned.根据 基本响应处理程序:
您可以捕获这种类型的异常(注意:您已经捕获了此异常的超类型
ClientProtocolException
),并且您可以在该 catch 块中放置一些自定义逻辑,以便在遇到错误时创建/保存一些响应情况,例如403。According to the documentation for BasicResponseHandler:
You could catch this type of exception (Note: you are already catching the supertype of this exception
ClientProtocolException
) and you could put some custom logic in that catch block to create / save some response when you encounter an error situation, such as the 403.