0

I have made a php file that posts the data to the db and using the android I should post some text. The problem is, When I submit the text A new data gets added to the database, but it's blank data. No numbers, text, etc ..

this is my code:

    public class mainactivity extends Activity {

        EditText edittext1;
        EditText edittext2;
        EditText edittext3;

        String name;
        String nationality;
        String sex;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            edittext1 = (EditText) findViewById(R.id.editText1);
            edittext2 = (EditText) findViewById(R.id.editText2);
            edittext3 = (EditText) findViewById(R.id.editText3);

            name = edittext1.getText().toString();
            sex = edittext3.getText().toString();
            nationality = edittext3.getText().toString();

        Button done = (Button) findViewById(R.id.button1);
        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                new Thread(new Runnable() {

                    @Override
                    public void run() {

                DefaultHttpClient httpclient= new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.xxx.com/thescript.php");
                Log.e("done 1st","its here");

                try
                {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("name", name));
                    nameValuePairs.add(new BasicNameValuePair("sex", sex));
                    nameValuePairs.add(new BasicNameValuePair("nationality", nationality));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    Log.e("done", String.valueOf(response));
                }
                catch(Exception e)
                {
                    Log.e("didn't work", "hardluck");
                    e.printStackTrace();
                }

            }
                }).start();
            }
        });

    }


}

logcat

09-24 20:13:50.302: E/done 1st(28390): its here
09-24 20:13:51.442: E/done(28390): org.apache.http.message.BasicHttpResponse@416c1b80

this is my current db,maybe the problem is in it

CREATE TABLE `people` (
  `name` varchar(20) COLLATE latin1_general_ci NOT NULL,
  `sex` varchar(10) COLLATE latin1_general_ci NOT NULL,
  `nationality` varchar(15) COLLATE latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
4

2 回答 2

1

我将假设您的 edittext 字段用于输入并且它正在等待用户输入。如果是这种情况,那么您可能希望在 ClickListener 中访问这些文本字段的值,而不是您的 create 方法。

我指的是这段代码

 name = edittext1.getText().toString();
 sex = edittext3.getText().toString();
 nationality = edittext3.getText().toString();

由于您在 onCreate 上检索这些值,因此用户尚未输入任何内容。用户应该先输入一些东西,然后点击你的按钮并启动 ClickListener。因此,您应该在 ClickListener 中包含上述代码。

于 2013-09-24T19:05:12.707 回答
1

您在 onCreate 方法中定义姓名、性别、国籍(当您的文本字段为空白时),请尝试在您的按钮侦听器中执行此操作:

        ..........
        name = edittext1.getText().toString();
        sex = edittext3.getText().toString();
        nationality = edittext3.getText().toString();

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name", name));
        nameValuePairs.add(new BasicNameValuePair("sex", sex));
        nameValuePairs.add(new BasicNameValuePair("nationality", nationality));
        ...........
于 2013-09-24T19:07:21.040 回答