duancan1900 2013-09-24 18:45
浏览 43
已采纳

Android向数据库发送数据变为空白

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;
  • 写回答

2条回答 默认 最新

  • dtxooq1020 2013-09-24 19:05
    关注

    I'm going to assume that your edittext fields are for input and that it's waiting for user input. If that is the case, then you would want to access the value of those text fields in your ClickListener instead of your create method.

    I'm referring to this code

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

    Since you are retrieving those values onCreate, the use has not entered anything yet. The user should enter something first, then hit your button and kick off the ClickListener. Thus, you should have the above code in your ClickListener instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!