从AndroidStudio向MySql插入记录仅在应用程序启动时有效

时间:2016-04-21 23:31:41

标签: android mysql

我有一个应用程序,它将某些参数插入存储在托管中的MySQL表中。

当我第一次启动应用程序并发送信息时,寄存器会成功添加到数据库中。然后我继续使用该应用程序。如果以后我尝试添加另一个注册表,没有任何反应,插入没有实现。如果我关闭(杀死)应用程序并再次启动它并再次发送插入它完美地工作。所以在简历中我只能在我第一次启动应用程序时发送插入内容。

我检查了调试,似乎没问题,如果在两种情况下都是相同的日志。

我的Android代码如下:  公共类AsyncCall扩展了AsyncTask {

这是调用AsyncTask的按钮:

sentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            parametros.add(new BasicNameValuePair("Categoria",intent.getStringExtra("Categoria")));
            parametros.add(new BasicNameValuePair("Titulo",intent.getStringExtra("Titulo")));
            parametros.add(new BasicNameValuePair("Latitud",Double.toString(latLng.latitude)));
            parametros.add(new BasicNameValuePair("Longitud",Double.toString(latLng.longitude)));
            parametros.add(new BasicNameValuePair("Cuerpo",descripcion.getText().toString()));
            parametros.add(new BasicNameValuePair("User_Id",String.valueOf(User_Id)));
            parametros.add(new BasicNameValuePair("Username",username));
            parametros.add(new BasicNameValuePair("Direccion",autoCompleteUbicacion.getText().toString()));
            parametros.add(new BasicNameValuePair("Tipo",tipo));
            parametros.add(new BasicNameValuePair("FechaInicio", strFechaInicio));
            parametros.add(new BasicNameValuePair("FechaFin", strFechaFin));

            AsyncCall task=new AsyncCall();
            task.execute();

        }
    });

这是doInBackground

    @Override
    protected Void doInBackground(String... params) {

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xxxxx/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(parametros));
            HttpResponse response = httpclient.execute(httppost);

        } catch (Exception e) {
            Log.e("log_tag", "Error in Http connection " + e.toString());
        }

        return null;
    }

我的问题:为什么插入仅在第一次执行时有效?我在第二次执行时检查了它的运行和参数是否正常,但没有在DB中插入任何内容

我的PHP文件:

<?php

    mysql_connect("localhost","xxxx","xxxx");
    mysql_select_db("xxxxx");

    $q=mysql_query("INSERT INTO xxxx_table 
              (xxxx_category, xxxx_title, xxxx_description, xxxx_type, 
               xxxx_location_lat, xxxx_location_lon, xxxx_direccion, 
               username,id_user, xxxx_start_date,xxxx_end_date) 
        VALUES ('".$_REQUEST['Categoria']."','".$_REQUEST['Titulo'].
                "','".$_REQUEST['Cuerpo']."','".$_REQUEST['Tipo'].
                "','".$_REQUEST['Latitud']."','".$_REQUEST['Longitud'].
                "','".$_REQUEST['Direccion']."','".$_REQUEST['Username'].
                "','".$_REQUEST['User_Id']."','".$_REQUEST['FechaInicio'].
                "','".$_REQUEST['FechaFin']."')");

    mysql_close();
   ?>

1 个答案:

答案 0 :(得分:-1)

我发现了错误,我发送了一个与BD中另一个表相关的空白参数。