尝试通过 PHP POST 方法提交 android 表单数据

我正在尝试创建一个通过 POST 方法发送信息的表单。


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();

    }

}

但是当我尝试导入 HttpClient、HttpPost、BaseNameValuePair 等时出现错误......


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;

请帮忙!!


摇曳的蔷薇
浏览 138回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java