Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpPost: how to get the result

Tags:

java

android

http

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.

like image 459
Sumit M Asok Avatar asked Feb 24 '10 04:02

Sumit M Asok


People also ask

Can we get data from Httppost?

Sending data with HTTP POST method The size and data type for HTTP POST requests is not limited. But you must specify the data type in the Content-Type header and the data size in the Content-Length header fields. The HTTP POST requests can also send data to the server using the URL parameters.

What is get and post method in Android?

GET and POST Two common methods for the request-response between a server and client are: GET- It requests the data from a specified resource. POST- It submits the processed data to a specified resource.

Can we do post using GET method?

POST is also more secure than GET , because you aren't sticking information into a URL. And so using GET as the method for an HTML form that collects a password or other sensitive information is not the best idea. One final note: POST can transmit a larger amount of information than GET .


2 Answers

Try to use the EntityUtil on your response:

String responseBody = EntityUtils.toString(response.getEntity()); 
like image 55
Moritz Avatar answered Sep 28 '22 13:09

Moritz


    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.

like image 24
Sumit M Asok Avatar answered Sep 28 '22 14:09

Sumit M Asok