Sunday

How to post data from android to php

I have try to post data from androi to php, here simple example how to do HTTP Post.

Http Client from Apache Commons is the way to go. It is already included in android. 

String   userName = etUserName.getText().toString();
	String   password = etPassword.getText().toString();
public void postDataToPHP() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.test.com/login.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair(un, userName ));
        nameValuePairs.add(new BasicNameValuePair(pwd, password ));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
 }
}

Happy Coding!!!

0 comments

Posts a comment

Related Posts  www.prandroid.com...
 
© 2011 Android Programming Tutorails | 2012 Templates
Designed by Blog Thiết Kế
Back to top