HttpPost获取Android响应代码

时间:2013-07-25 22:02:43

标签: android http

我正在使用

HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

以便将数据发送到我的webservice。我怎么能得到http响应代码(如200,404,500等)?

我无法使用getResponseCode()

中的HttpURLConnection

2 个答案:

答案 0 :(得分:1)

通过

HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int code = statusline.getStatusCode();

答案 1 :(得分:1)

您的更多代码在这里会有所帮助,但我这样做的方式是:

HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

HttpResponse response = client.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode()