0

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) {

            }   

    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – LordRaydenMK Jul 03 '14 at 12:04

2 Answers2

0

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();
joselufo
  • 3,393
  • 3
  • 23
  • 37
0

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) {

            } 



    }
    }
Ajay Parsana
  • 175
  • 2
  • 2
  • 14