tags:

views:

1769

answers:

3

I have been trying long to send an HttpPost request and retrieve response but even though I was able to make a connection I don't yet get how to get the string message which is returned by the request-response

    HttpClient httpclient = new DefaultHttpClient();   
HttpPost httppost = new HttpPost("http://www.myurl.com/app/page.php"); 
    // Add your data   
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);   
    nameValuePairs.add(new BasicNameValuePair("type", "20"));
    nameValuePairs.add(new BasicNameValuePair("mob", "919895865899"));
    nameValuePairs.add(new BasicNameValuePair("pack", "0"));
    nameValuePairs.add(new BasicNameValuePair("exchk", "1"));

    try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.d("myapp", "works till here. 2");
            try {
                HttpResponse response = httpclient.execute(httppost);
                Log.d("myapp", "response " + response.getEntity());
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

I'm sorry, I sound very naive because I'm new to java. Please help me.

+1  A: 
    URL url;
    url = new URL("http://www.url.com/app.php");
    URLConnection connection;
    connection = url.openConnection();
    HttpURLConnection httppost = (HttpURLConnection) connection;
    httppost.setDoInput(true);
    httppost.setDoOutput(true);
    httppost.setRequestMethod("POST");
    httppost.setRequestProperty("User-Agent", "Tranz-Version-t1.914");
    httppost.setRequestProperty("Accept_Language", "en-US");
    httppost.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");
    DataOutputStream dos = new DataOutputStream(httppost.getOutputStream());
    dos.write(b); // bytes[] b of post data

    String reply;
    InputStream in = httppost.getInputStream();
    StringBuffer sb = new StringBuffer();
    try {
        int chr;
        while ((chr = in.read()) != -1) {
            sb.append((char) chr);
        }
        reply = sb.toString();
    } finally {
        in.close();
    }

This code snippet works. I got it after along search , but from a J2ME code.

Sumit M Asok
+5  A: 

Try to use the EntityUtil on your response:

String responseBody = EntityUtils.toString(response.getEntity());

Soulreaper
Hi Soulreaper, Thanks for your response . But why is EntityUtils used ?
Sumit M Asok
It is just a convenient way to extract the contained raw byte answer into string form.
Soulreaper
+1  A: 

You can call execute method with a ResponseHandler. Here's an example:

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpClient.execute(httppost, responseHandler);
Sarp Centel
Hi Sarp, Thanks for your response . But, what is the advantage of using this ?
Sumit M Asok