我正在尝试创建一个通过 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;请帮忙!!
添加回答
举报
0/150
提交
取消