2017-09-08 44 views
0

私はNo value for JSONObjectエラーを解決しようとしています。私はJSON応答を取得しており、tryブロックを使用してJSONObjectを作成しています。私は、JSONのREPONSEが、私はこの乗り越えることができますどのように{"success":false,"messages":"Incorrect email\/password combination"}JSONObjectの値がありません

よう

であることを意味している資格情報に間違ったログをテストしています。ここに私のコードです。

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はif(jObj.has("user")内部場合、ブロック内のコードのブロックを入れて試みたが、エラーが表示されていません。プログレスダイアログを示して、すぐ

+0

コードのどの時点で問題が発生するのですか? –

+0

@AalapPatelは、試行ブロック – facilitator

+0

の直前に受信していた '{" success ":false、" messages ":" Incorrect email \/password combination "}' –

答えて

1

を隠しあなたは成功がfasleのときの値を取得しようとしている...成功は真であるときにそれを取得しよう

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 
} 
+0

ありがとう..yesは完璧に動作します。論理的な誤りでした。 APIでは、エラーではなく成功を使用しているので、エラーを成功に変更して、if(エラー)ではなくif(成功) – facilitator

0

私は何かを追加したいと思いますAkshayの答え、ベストプラクティスのために、常に応答が成功するかどうかをチェックするようにしてください。このように..

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.. 

    } 
関連する問題