[英]Post data to php server in android
我想將數據發布到php服務器。 但是,當我單擊按鈕時,它不起作用。 我已經在Manifest.xml中添加了Internet權限。
我的代碼有什么問題?
public class MainActivity extends Activity {
Button sendButton;
EditText msgTextField, msgTextField2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
msgTextField = (EditText) findViewById(R.id.msgTextField);
msgTextField2 = (EditText) findViewById(R.id.msgTextField2);
sendButton = (Button) findViewById(R.id.send);}
public void send(View v) { //View v ?
String msg = msgTextField.getText().toString();
String msg2 = msgTextField2.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://blahblah.com/myphppage");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("frm_ad", msg));
nameValuePairs.add(new BasicNameValuePair("frm_nomre", msg2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
msgTextField.setText("");
msgTextField2.setText("");
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
}
您在UIThread中沒有一個http請求。 您已使用其他Thread如何進行Asynctask。
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://blahblah.com/myphppage");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("frm_ad", msg));
nameValuePairs.add(new BasicNameValuePair("frm_nomre", msg2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
//settext() Textview
super.onPostExecute(aVoid);
}
}.execute();
試試下面的代碼:
EditText msgTextField, msgTextField2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
msgTextField = (EditText) findViewById(R.id.msgTextField);
msgTextField2 = (EditText) findViewById(R.id.msgTextField2);
sendButton = (Button) findViewById(R.id.send);
sendButton.setOnClickListener(send)
}
private OnClickListener send = new OnClickListener() {
@Override
public void onClick(final View v) {
String msg = msgTextField.getText().toString();
String msg2 = msgTextField2.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://blahblah.com/myphppage");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("frm_ad", msg));
nameValuePairs.add(new BasicNameValuePair("frm_nomre", msg2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
msgTextField.setText("");
msgTextField2.setText("");
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.