Home > Backend Development > PHP Tutorial > How to Send POST Data in Android Using AsyncTask and Older Methods?

How to Send POST Data in Android Using AsyncTask and Older Methods?

Linda Hamilton
Release: 2024-12-19 02:31:13
Original
223 people have browsed it

How to Send POST Data in Android Using AsyncTask and Older Methods?

How to Send POST Data in Android

In Android development, it is common to exchange data with remote servers. A common method of communication is through HTTP POST requests. This article will guide you through the process of sending POST data to a PHP script and displaying the result.

Android API Level Compatibility

The provided code sample works on Android 6.0 and above. For Android devices running versions prior to 6.0, an updated answer is provided.

Using AsyncTask

public class CallAPI extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        String urlString = params[0];
        String data = params[1];

        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
            writer.write(data);
            writer.flush();
            writer.close();
            out.close();
            urlConnection.connect();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return null;
    }
}
Copy after login

Using Apache Http Client (Outdated)

public void postData() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
}
Copy after login

Note: The Apache Http Client solution is outdated and only works on Android devices up to 5.1. For Android 6.0 and above, use the updated code sample provided in the first response.

The above is the detailed content of How to Send POST Data in Android Using AsyncTask and Older Methods?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template