【问题标题】:Insert Data Into MysqlDatabase through JSON Request通过 JSON 请求将数据插入 MysqlDatabase
【发布时间】:2015-09-20 08:48:57
【问题描述】:

您好,我正在尝试根据用户评分将数据插入Mysql 数据库。但我得到 1 个错误:

异步任务:

private class MyInsertDataTask extends AsyncTask<String, Void, String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(UserProfile.this);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setIndeterminate(true);
            pDialog.setMessage("Please Wait...");
            pDialog.setCancelable(false);
            pDialog.setInverseBackgroundForced(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("magazi_id", "" + 3));
            nameValuePairs.add(new BasicNameValuePair("ratingNumber", String.valueOf(percent)));
            nameValuePairs.add(new BasicNameValuePair("comment",comTxt));

            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://my.chatapp.info/order_api/insertData/insert.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.e("pass 1", "connection success ");
            }
            catch(Exception e)
            {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address",
                        Toast.LENGTH_LONG).show();
            }

            try
            {
                BufferedReader reader = new BufferedReader
                        (new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
                Log.e("pass 2", "connection success ");
            }
            catch(Exception e)
            {
                Log.e("Fail 2", e.toString());
            }

            try
            {
                JSONObject json_data = new JSONObject(result);
                code=(json_data.getInt("code"));
                if(code==1)
                {
                    Toast.makeText(UserProfile.this, "Inserted Successfully",
                            Toast.LENGTH_SHORT).show();
                    return String.valueOf(code);
                }
                else
                {
                    Toast.makeText(UserProfile.this, "Sorry, Try Again",
                            Toast.LENGTH_LONG).show();
                    return String.valueOf(code);
                }
            }
            catch(Exception e)
            {
                Log.e("Fail 3", e.toString());
            }

            return null;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);
            pDialog.dismiss();
        }
    }

插入方式:

private void insertDataToDatabaseFromRating() {
        ratingBar = (RatingBar) newLayout.findViewById(R.id.ratingBar);
        ratingComment = (EditText) newLayout.findViewById(R.id.ratingComment);
        percent = ratingBar.getRating();
        comTxt = ratingComment.getText().toString();
        if (comTxt.isEmpty()) {
            comTxt = " ";
        }

        MyInsertDataTask task = new MyInsertDataTask();
        task.execute();
    }

我插入数据的 .php 文件

<?php
try {
    $handler = new PDO('mysql:host=localhost;dbname=project', 'username', 'password');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
    echo $e->getMessage();
    die();
}


$magaziId = $_REQUEST['magazi_id'];
$rating=$_REQUEST['ratingNumber'];
$comment = $_REQUEST['comment'];
$flag['code']=0;
if($handler->query('INSERT INTO ratings VALUES(, $magaziId, $rating, $comment)'){
$flag['code']=1;
}
print(json_encode($flag));
?>

错误:

09-20 04:43:11.470    7318-7350/com.order.app.order E/pass 1﹕ connection success
09-20 04:43:11.470    7318-7350/com.order.app.order E/pass 2﹕ connection success
09-20 04:43:11.470    7318-7350/com.order.app.order E/Fail 3﹕ org.json.JSONException: End of input at character 0 of

我通过了 1 和 2,但它失败了。谁能帮帮我?

谢谢

【问题讨论】:

标签: php android mysql json


【解决方案1】:

像这样更改您的代码:

.php 文件:

<?php
try {
    $handler = new PDO('mysql:host=localhost;dbname=project', 'username', 'password');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
    echo $e->getMessage();
    die();
}


    $magaziId = $_POST['magazi_id'];
    $rating=$_POST['ratingNumber'];
    $comment = $_POST['comment'];
    $handler->query("INSERT INTO ratings(id, magazi_id, ratingNumber, comment) VALUES('', '$magaziId','$rating','$comment')");
    die();
?>

你的异步任务是这样的:

private class MyInsertDataTask extends AsyncTask<String, Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(UserProfile.this);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setIndeterminate(true);
            pDialog.setMessage(getString(R.string.dialog_rate_data_submit));
            pDialog.setCancelable(false);
            pDialog.setInverseBackgroundForced(true);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            nameValuePairs = new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("magazi_id", "" + 3));
            nameValuePairs.add(new BasicNameValuePair("ratingNumber", String.valueOf(percent)));
            nameValuePairs.add(new BasicNameValuePair("comment", comTxt));
            try
            {
                httpClient = new DefaultHttpClient();
                httpPost = new HttpPost("http://my.chatapp.info/order_api/insertData/insert.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpClient.execute(httpPost);
                httpEntity = response.getEntity();
                is = httpEntity.getContent();
            }
            catch(Exception e)
            {
                Log.e("Fail 1", e.toString());
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            pDialog.dismiss();
        }
    }
}

希望对你有帮助!!!

【讨论】:

    猜你喜欢
    • 2016-06-05
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2014-11-13
    相关资源
    最近更新 更多