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;

Search & Download lots of Eclipse-Android Tutorials:-
  Using Pdf Wallet you can find useful and free Android Tutorials.


Please Select File-Format

PDF  - eBooks in pdf format.
PPT  - Presentations:2003.
PPTX- Presentations:2007+.
DOC - Microsoft Word Files:2003.
DOCXMicrosoft Word Files:2007+.
XLS  - Excel File format:2003.
XLSX - Excel File Format:2007+.
CHM - Compiled Help Files.
RTF  - Rich Text format.
TXT  - Text Files. 

No comments:

Post a Comment