Gary
Gary

Reputation: 513

Error parsing data json ...

I am working on an android app and trying to allow users to login via a php/mysql webservice. The app however, is crashing after entering email and password. Checking the logcat I see the following ...

The JSON shows up as -- nnn{"success":"1","uid":"111","name":"test","email":"test"}n

Then the error -- Error parsing data org.json.JSONException: Value nnn of type java.lang.String cannot be converted to JSONObject

What or where is this "nnn" coming from ? I cant spot this in my code (thought it might be a typo)

Here is the php -->

// get tag
if (isset($_POST['tag']) && $_POST['tag'] != '') {
$tag = $_POST['tag'];

//json response array
$response = array();

// check for tag type
if ($tag == 'login') {

$email = $_POST['email'];
$password = $_POST['password'];

//check user exists
$qry = "SELECT * FROM users WHERE email = ? AND password = ? ";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);

if($stmt->execute()){

$result = $stmt->get_result();

$user = $result->fetch_assoc();

        // if user exists, set json values
        $response["success"] = "1";
        $response["uid"] = $user["unique_id"];
        $response["name"] = $user["name"];
        $response["email"] = $user["email"];
        echo json_encode($response);
    } else {
        // user not found
        $response["error"] = 1;
        $response["error_msg"] = "Incorrect email or password!";
        echo json_encode($response);
        exit;
    }
    // else register new user
} else if ($tag == 'register') {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    // first check if user already exists

    $query = "SELECT email FROM users WHERE email=?";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param('s',$email);
    $stmt->execute();
    /* store result */
    $stmt->store_result();
    if ($stmt->num_rows > 0){
       //if result greater than 0 , email exists
        $response["error"] = 2;
        $response["error_msg"] = "Email address already registered.";
        echo json_encode($response);
        exit;
    } else {
        // create user
        $qry = "INSERT INTO users (name, email, password) VALUES(?, ?, ?)";
        $stmt = $mysqli->prepare($qry);
        $stmt->bind_param('sss', $name, $email, $password);

        if($stmt->execute())
        {
            $uid = mysql_insert_id();
            // user stored successfully
            $response["success"] = 1;
            $response["uid"] = $uid;
            $response["name"] = $name;
            $response["email"] = $email;
            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = 1;
            $response["error_msg"] = "Error with Registartion";
            echo json_encode($response);
        }
    }
} else {
    echo "Invalid Request";
}
 } else {
 echo "Access Denied";
}
?>

Many thanks.

Upvotes: 0

Views: 241

Answers (1)

Gary
Gary

Reputation: 513

Turns out is was a typo in my JSONParser

while ((line = reader.readLine()) != null) { sb.append(line + "n");

Changed to \n and the error disappeared.

Thanks.

Upvotes: 1

Related Questions