douqi1625 2015-03-01 17:13
浏览 63

获取DatePicker值以存储到Android的php mysql中

Good Day Everyone!

I am currently working on how to put a date value into my php mysql using android's date picker. My plan is to get data from a datepicker and what ever its value is it will be put into mysql. For some reason, there are no errors but it just input the value '0000-00-00' into mysql everytime I put it into my database. The following is my java android code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_datetime);

    //setup strict mode policy
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    timePicker = (TimePicker) findViewById(R.id.timePicker);
    timePicker.setIs24HourView(true);
    datePicker = (DatePicker) findViewById(R.id.datePicker);
    //lblTime = (TextView) findViewById(R.id.lblTime);
    btnAddTime = (Button) findViewById(R.id.btnAddTime);

    btnAddTime.setOnClickListener(new View.OnClickListener() {

        InputStream is = null;
        @Override
        public void onClick(View v) {

            //String time = lblTime.getText().toString();
            //String time = timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute() + ":" + "00";
            String date = datePicker.getYear() + "-" + datePicker.getMonth() + 1 + "-" + datePicker.getDayOfMonth();


            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

            nameValuePairs.add(new BasicNameValuePair ("date", date));

            try {

                //setting up the default http client
                HttpClient httpClient = new DefaultHttpClient();

                //setting up the http post method and passing the url in case
                //of online database and the ip address in case of localhost database
                //and the php file which serves as a link between the android app
                //and the database.
                HttpPost httpPost = new HttpPost("http://10.0.2.2/add_time.php");

                //passing the nameValuePairs inside the httppost
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                //getting the response
                HttpResponse response = httpClient.execute(httpPost);

                //setting up the entity
                HttpEntity httpEntity = response.getEntity();

                //setting up the content inside an input stream reader
                //lets define the input stream reader
                is = httpEntity.getContent();

                //Toast message if the data is successfully entered
                Toast.makeText(getApplicationContext(), "Date entered succesfully", Toast.LENGTH_LONG).show();
            }
            catch (ClientProtocolException e) {

                Log.e("ClientProtocol", "Log_tag");
                e.printStackTrace();
            }
            catch (IOException e) {
                Log.e("Log tag", "IOException");
                e.printStackTrace();

            }
        }

    });

The following is my php code that I have:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);

$con = mysql_connect('localhost', 'root', '');
mysql_select_db('testdb', $con);

$date = $_POST['date'];

mysql_query("Insert into datetable (date) values('{$date}')");


?>

I will greatly appreciate is someone could help me with this Thanks!

  • 写回答

2条回答 默认 最新

  • dousi2251 2015-03-01 17:19
    关注

    First You not calling

     datePicker.init(year, month, day, dateSetListener);
    

    Second you have not implemented OnDateChangedListener

      private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {
    
            public void onDateChanged(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
    
    // Use these values to pass....
    
            }
        };
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器