How to send POST data from Android

Asked 2 years ago, Updated 2 years ago, 89 views

I've dealt with a lot of script languages like PHP and JavaScript. I have little experience with Java and Android.

I want to send POST data through PHP script and show it on the screen, what should I do?

php httpconnection java client-server android

2022-09-22 21:28

1 Answers

Using the Http Client in the Apache Commons library is easy. It's already included in Android. If you follow the example below, you can do HTTP Post simply.

public void postData() {
    // // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    httpPost httppost = new HttpPost ("http://www.site address.com/script.php"));

    try {
        // Use the appropriate data format as shown below 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        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
    }
} 


2022-09-22 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.