I have been trying to solve No value for JSONObject error without any success. I am getting the JSON response and using a try block to create the JSONObject. I am testing for incorrect log in credentials which means the JSON reponse is like
{"success":false,"messages":"Incorrect email\/password combination"}
How can I get past this. Here's my code.
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("success");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
mSessionManager.setLogin(true);
// Now store the user in SQLite
JSONObject user = jObj.getJSONObject("user");
String firstName = user.getString("firstname");
String lastName = user.getString("lastname");
String email = user.getString("email");
String created_at = user
.getString("created_at");
String uid = String.valueOf(user.getInt("user_id"));
// Inserting row in users table
mSQLiteHandler.addUser(firstName, lastName, email, uid, created_at);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
Pedometer.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("messages");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
Toast.makeText(getApplicationContext(),
"Json error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
I have tried putting the block of code inside the if block inside if(jObj.has("user") but no error is being shown. The progress dialog shows and hides immediately
2 Answers 2
you are trying to get values when success is fasle ... try to retrieve it when success is true
try {
change ----> JSONObject jObj = new JSONObject(response.body().toString);
boolean error = jObj.getBoolean("success");
// Check for error node in json
change ----> if (error) {
//retrieve values here
}
I would like to add something to Akshay's answer, For best practice you should always try to check whether response is successful or not. Like this..
if(response.isSuccessful()){
String responseBody = response.body().string();
try {
JSONObject jObj = new JSONObject(responseBody); //here was the cause mentioned by Akshay
boolean error = jObj.getBoolean("success");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
mSessionManager.setLogin(true);
// Now store the user in SQLite
JSONObject user = jObj.getJSONObject("user");
String firstName = user.getString("firstname");
String lastName = user.getString("lastname");
String email = user.getString("email");
String created_at = user
.getString("created_at");
String uid = String.valueOf(user.getInt("user_id"));
// Inserting row in users table
mSQLiteHandler.addUser(firstName, lastName, email, uid, created_at);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
Pedometer.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("messages");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
Toast.makeText(getApplicationContext(),
"Json error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
else{
//here you should observe error caused..
}
{"success":false,"messages":"Incorrect email\/password combination"}which is been received right before the try block