通过POST方法提交表格

时间:2015-05-07 17:31:31

标签: android forms http post

发送消息时,我收到应用程序错误,应用程序停止。我想使用POST方法提交表单。请帮我修改代码,因为我是android的新手。

我已从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();
            }
        }

1 个答案:

答案 0 :(得分:0)

您正在主线程上进行网络操作,需要在单独的线程中完成。 做这样的事情:

要知道如何使用AsyncTask并设置其参数,请参阅:  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();
        }
    }