简体   繁体   中英

Trying to submit android form data via PHP POST METHOD

I'm trying to create a form which is to send the information via a POST method.

public class PostData extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.yourdomain.com/serverside-script.php");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "01"));
            nameValuePairs.add(new BasicNameValuePair("message", params[0]));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            try {
                HttpResponse response = httpclient.execute(httppost);
                String op = EntityUtils.toString(response.getEntity(), "UTF-8");//The response you get from your script
                return op;
            } catch (IOException e) {
                e.printStackTrace();
            }
            //reset the message text field
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        msgTextField.setText("");
        Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_SHORT).show();
    }
}

But I am getting errors when I try to import HttpClient, HttpPost, BaseNameValuePair etc....

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

Please help!!

HttpClient was deprecated in API Level 22 and removed in API Level 23 . You have to use HttpURLConnection .

Docs how to use:-

https://developer.android.com/reference/java/net/HttpURLConnection

Example:-

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

Uri.Builder builder = new Uri.Builder()
        .appendQueryParameter("id", "01")
        .appendQueryParameter("message", params[0]);
String query = builder.build().getEncodedQuery();

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();

conn.connect();

But, If you still want to use it, then you need to add additional dependencies for it.

you need to use okhttp rather than http clinet check here for okhttp post data.

https://www.studytutorial.in/android-okhttp-post-and-get-request-tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM