I am trying to retrieve a vat number from my database to display when the user logs in. It was working fine and then all of a sudden stopped working and started to return the error:
org.json.JSONException: Value null at vat of type org.json.JSONObject$1 cannot be converted to int
and I'm unsure as to why. Any help would be greatly appreciated.
Here is the code:
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success =jsonResponse.getBoolean("success");
if (success) {
int vat = jsonResponse.getInt("vat");
Intent intent = new Intent(LoginActivity.this, ChoiceSelectActivity.class);
intent.putExtra("username", username);
intent.putExtra("vat", vat);
LoginActivity.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage ("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
And here is the PHP file:
{<?php
$con = mysqli_connect("xxx.com", "xxx", "xxx", "xxx");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM Name WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $username, $password, $vat);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["username"] = $username;
$response["password"] = $password;
$response["vat"] = $vat;
}
echo json_encode($response);
?>
password_hash()andpassword_verify()please use them. And here are some good ideas about passwords If you are using a PHP version prior to 5.5 there is a compatibility pack available here