Dec 7, 2011

How to send Post data to PHP server in android

public boolean PostData() {
try {
// creating default Client
HttpClient mClient = new DefaultHttpClient();
// Connect URL
StringBuilder sb=new StringBuilder("URL_OF_WEB_SERVER");
// Establishing post connection to Specified URL
HttpPost mpost = new HttpPost(sb.toString());
// NameValuePair : A simple class encapsulating an attribute/value pair.
// List Size is 8 atributes
List nameValuepairs = new ArrayList(8);
// adding attributes to List
nameValuepairs.add(new BasicNameValuePair(name1,val1));
nameValuepairs.add(new BasicNameValuePair(name2,val2));
nameValuepairs.add(new BasicNameValuePair(name3,val3));
nameValuepairs.add(new BasicNameValuePair(name3,val3));
nameValuepairs.add(new BasicNameValuePair(name4,val4);
nameValuepairs.add(new BasicNameValuePair(name5,val5));
nameValuepairs.add(new BasicNameValuePair(name6,val6);
nameValuepairs.add(new BasicNameValuePair(name7,val7));
// UrlEncodedFormEntity :An entity composed of a list of url-encoded pairs. This is typically
// useful while sending an HTTP POST request.
mpost.setEntity(new UrlEncodedFormEntity(nameValuepairs));
// excute request and get response
HttpResponse responce = mClient.execute(mpost);
// get response content
HttpEntity entity = responce.getEntity();
// convert stream to String
BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder sb1 = new StringBuilder();
String line = null;
while ((line = buf.readLine()) != null) {
Toast.makeText(getApplicationContext(), sb1.toString()+"",1).show();
isPosted = true;
} catch (UnsupportedEncodingException e) {
Log.w(" error ", e.toString());
} catch (Exception e) {
Log.w(" error ", e.toString());
return isPosted;


  1. This comment has been removed by a blog administrator.

  2. This comment has been removed by a blog administrator.