1

我面临 json 解析器错误,我不知道是什么原因导致它,因为有时它工作正常,有时错误只是弹出并且我的数据没有更新到数据库中。数据将被发布到 php 文件以加密并更新到数据库。有时它可以将数据插入数据库,当它不能时,它会显示 json 解析错误。

这是我的错误:

08-31 01:10:55.598: E/JSON Parser(360): Error parsing data org.json.JSONException: End of input at character 0 of 

这是我的代码:

String email = db.getEmail();
            String currentPassword = cpCurrentPassword.getText().toString();
            String newPassword = cpNewPassword.getText().toString();

                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("email", email));
                        params.add(new BasicNameValuePair(
                                "currentpassword", currentPassword));
                        params.add(new BasicNameValuePair("newpassword",
                                newPassword));
                                JSONObject json = jsonParser
                                        .makeHttpRequest(
                                                newpassword_url, "POST",
                                                params);

                                try {
                                    int success1 = json1
                                            .getInt(TAG_SUCCESS);
                                    if (success1 == 1) {    
                                        showToast("Password successfully changed!");

这是我的PHP:

<?php
// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['email']) && isset($_POST['newpassword'])){
$email = $_POST['email'];
$newpassword = $_POST['newpassword'];
}
// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

$result = mysql_query("UPDATE users SET encrypted_password = '$newpassword' WHERE email = '$email'");

// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Password successfully changed";

// echoing JSON response
echo json_encode($response);
}
?>

即使数据没有更新到数据库,我的成功标签仍然会响应 1。这是我不明白的地方。

4

1 回答 1

2

尝试更改参数的名称。

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("currentpassword", currentPassword));
params.add(new BasicNameValuePair("newpassword", newPassword));

有了这个

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email1", email));
params.add(new BasicNameValuePair("currentpassword1", currentPassword));
params.add(new BasicNameValuePair("newpassword1", newPassword));

并在 php 代码中更改它

$email = $_POST['email1'];
$newpassword = $_POST['newpassword1'];

让我们看看这是否有效

于 2014-08-17T14:59:24.127 回答