我正在嘗試創建一個通過 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
提交
取消