I want to post my data to the php server. But it doesn't work when I click the button. I`ve added Internet permission in Manifest.xml.
What is problem in my code ?
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) {
}
}
}
You don't have one http request in the UIThread. You have use other Thread how 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();
Try below code:
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) {
}
}
}
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.