0

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

asked Sep 8, 2017 at 20:02
3
  • at what point in the code u experience issue please point it ?? Commented Sep 8, 2017 at 20:05
  • @AalapPatel after getting the response {"success":false,"messages":"Incorrect email\/password combination"} which is been received right before the try block Commented Sep 8, 2017 at 20:09
  • are u using retrofit ? Commented Sep 8, 2017 at 20:10

2 Answers 2

1

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
}
answered Sep 8, 2017 at 20:09
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks..yes works perfectly. Was a logical error. In the api I am using success and not error so does make sense to change error to success so that it is if(success) and not if(error)
0

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..
 }
answered Sep 8, 2017 at 20:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.