0

I have a piece of code that sends post packet to somewhere. I want to output to my screen the status code, eg 200 for OK, or 400 for bad request.. either the number or the string, what i have at the moment:

response.getStatusLine().getStatusCode()

where response is an HttpResponse, but i need to compare it to HttpStatus.SC_OK or whatever. the post is happening inside an AsyncTask class, which executes the post.execute() object. the return value is some "com.example.....postAsync@#NUMBER#". the number is something random, not a response type. what should i do?

Edit: Some more code:

HttpResponse response = null;
StringEntity tmp = null;
HttpPost httpPost = null;
DefaultHttpClient httpClient;
HttpContext localContext;
    public String post(String url,String data) {
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(url);
        localContext = new BasicHttpContext(); 
        String ret = "";
        try {
            tmp = new StringEntity("value={ \"n\": " + data + " }","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpPost.setEntity(tmp);
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
        try {
            response = httpClient.execute(httpPost,localContext);
            if (response != null) {
                ret = "" + response.getEntity();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

Its like im getting the content of the response, where im only interested if its sent ok or not.

4

1 回答 1

0

试试这样:

response = httpClient.execute(httpPost,localContext);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode(); // This will be 200 or 404, etc.
于 2013-02-09T11:53:09.697 回答