Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading HttpPost response

Tags:

java

android

http

I'm using this code to post a request to a http server:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost( "http://192.168.0.1/test.php" );
HttpResponse response = null;
try {
    List< NameValuePair > nameValuePairs = new ArrayList< NameValuePair >( 1 );
    nameValuePairs.add( new BasicNameValuePair( "num", "2" ) );
    post.setEntity( new UrlEncodedFormEntity( nameValuePairs ) );
    response = client.execute( post );
}
catch( ClientProtocolException e ) {
    ...
}
catch( IOException e ) {
    ...
}

The response is nothing more than a simple String. How can I read this response as a String? It doesn't seem like HttpResponse have a method for doing this directly.

like image 441
Espen Avatar asked Dec 05 '10 22:12

Espen


People also ask

What is the response of a post request?

HTTP POST request A POST request requires a body in which you define the data of the entity to be created. A successful POST request would be a 200 response code.

How do I get http body response?

To get the response body as a string we can use the EntityUtils. toString() method. This method read the content of an HttpEntity object content and return it as a string. The content will be converted using the character set from the entity object.

How to GET response from Http request in Java?

Set the request method in HttpURLConnection instance, default value is GET. Call setRequestProperty() method on HttpURLConnection instance to set request header values, such as “User-Agent” and “Accept-Language” etc. We can call getResponseCode() to get the response HTTP code.

What is Http request and Http response with example?

HTTP works as a request-response protocol between a client and server. Example: A client (browser) sends an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.


2 Answers

I have created this helper method for sending data and special headers by POST method in Android (headers HashMap could be empty if you do not have any custom headers):

public static String getStringContent(String uri, String postData, 
        HashMap<String, String> headers) throws Exception {

        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost();
        request.setURI(new URI(uri));
        request.setEntity(new StringEntity(postData));
        for(Entry<String, String> s : headers.entrySet())
        {
            request.setHeader(s.getKey(), s.getValue());
        }
        HttpResponse response = client.execute(request);

        InputStream ips  = response.getEntity().getContent();
        BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
        if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK)
        {
            throw new Exception(response.getStatusLine().getReasonPhrase());
        }
        StringBuilder sb = new StringBuilder();
        String s;
        while(true )
        {
            s = buf.readLine();
            if(s==null || s.length()==0)
                break;
            sb.append(s);

        }
        buf.close();
        ips.close();
        return sb.toString();

 } 
like image 167
Aliostad Avatar answered Oct 10 '22 16:10

Aliostad


response.getStatusLine();// For reading status line

org.apache.http.util.EntityUtils.toString(response.getEntity());
like image 25
Ratna Dinakar Avatar answered Oct 10 '22 16:10

Ratna Dinakar