1

当我使用以下代码时,我可以获得数据:

    private static String getHttpPostContent(String address, String token) {    
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(address);

            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(new BasicNameValuePair("token", token));
            post.setEntity(new UrlEncodedFormEntity(pairs));

            HttpResponse httpResponse = client.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            String serverResponse = EntityUtils.toString(entity);

            return serverResponse;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

根据我的文档,如果发生错误,则服务器发送错误 418 告诉我发生了错误。我不知道如何抓住它?

当我使用 HTTP GET 方法时,有一种方法可以检查响应代码,如下所示:

int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) 
...

HTTP POST 方法中是否有类似的方法?

4

1 回答 1

0

你试过观察httpResponse.getStatusLine().getStatusCode()吗?

于 2013-04-03T09:37:01.387 回答