0

While sending the message I got application error and the app stops. I wanted to submit the form using POST method. Please help me to correct the code as i am new to android.

I have taken code reference from http://www.onlymobilepro.com/2013/03/16/submitting-android-form-data-via-post-method/

public class MainActivity extends Activity {

    EditText msgTextField;
    Button sendButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.form);

            //make message text field object
            msgTextField = (EditText) findViewById(R.id.msgTextField);
            //make button object
            sendButton = (Button) findViewById(R.id.sendButton);
        }

        public void send(View v)
        {
            //get message from message box
            String  msg = msgTextField.getText().toString();

            //check whether the msg empty or not
            if(msg.length()>0) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.yourdomain.com/serverside-script.php");

                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                       nameValuePairs.add(new BasicNameValuePair("id", "01"));
                       nameValuePairs.add(new BasicNameValuePair("message", msg));
                       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                       httpclient.execute(httppost);
                        msgTextField.setText(""); //reset the message text field
                        Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                //display message if text field is empty
                Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show();
            }
        }
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • What error do you get? Error message and stack trace (if available) would help very much! – siegi May 07 '15 at 17:37
  • When I run the app It says "Application Error Unfortunately, the app has stopped" – mukesh phunyal May 07 '15 at 17:43
  • If you connect your phone to your computer you can read Androids log files with more details about the error using [logcat](http://developer.android.com/tools/help/logcat.html). The IDE you use to develop the application probably has a built in logcat viewer (e.g. see [this question for using logcat in Eclipse](http://stackoverflow.com/q/3280051/1347968)). – siegi May 07 '15 at 17:47
  • Another copy/paste dev ? – 2Dee May 07 '15 at 18:00
  • Well you can say that for now but beginner actually :p – mukesh phunyal May 07 '15 at 18:03

1 Answers1

0

You are doing network operation on Main Thread , it needs to be done in seperate Thread . Do something like this:

To know how to use AsyncTask and set its parameters, see this : What arguments are passed into AsyncTask<arg1, arg2, arg3>?

EditText msgTextField;
Button sendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.form);

    //make message text field object
    msgTextField = (EditText) findViewById(R.id.msgTextField);
    //make button object
    sendButton = (Button) findViewById(R.id.sendButton);
}

public void send(View v) {
    //get message from message box
    String msg = msgTextField.getText().toString();
    if (!msg.isEmpty()) {
        new PostData().execute(msg);
    } else {
        //display message if text field is empty
        Toast.makeText(getBaseContext(), "All fields are required", Toast.LENGTH_SHORT).show();
    }
}

    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();
        }
    }
Community
  • 1
  • 1
Nikhil Verma
  • 1,777
  • 1
  • 20
  • 41