Jones Ch
Jones Ch

Reputation: 65

JSON Parse: Error parsing data org.json.JSONException: End of input character 0 of

Im facing json parser error which i dont know what causes it because sometime its working fine sometimes the error just pop up and my data was not updated into the database. The data will be post to the php file to encrypt and update to the database. Sometime it can insert the data into database and when it cannot, it will have the json parsing error shown.

This is my ERROR:

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

This is my CODE:

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!");

This is my 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);
}
?>

Even if the data was not updated to the database, my success tag will still response 1. This is where i dont understand.

Upvotes: 1

Views: 1690

Answers (1)

Danu
Danu

Reputation: 55

Try to change the name of parameter.

// 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));

With this

// 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));

And also change it in php code

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

Let's see if this work or not

Upvotes: 2

Related Questions