JSON解析:解析数据时出错org.json.JSONException:输入字符0的结尾

时间:2012-08-30 17:31:00

标签: java android json

我面临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.这是我不明白的地方。

1 个答案:

答案 0 :(得分: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'];

让我们看看这项工作是否成功