没有JSONObject Android的价值

时间:2017-09-08 20:02:05

标签: android json

我一直试图解决No value for JSONObject错误而没有任何成功。我正在获取JSON响应并使用try块来创建JSONObject。我正在测试不正确的登录凭据,这意味着JSON响应就像

{"success":false,"messages":"Incorrect email\/password combination"}

我怎样才能解决这个问题。这是我的代码。

  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();
    }

我已经尝试将代码块放在if(jObj.has("user")内的if块中,但是没有显示错误。进度对话框会立即显示和隐藏

2 个答案:

答案 0 :(得分:1)

  

当你成功的时候,你正试图获得价值......当成功是真的时,试着找回它

 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
}

答案 1 :(得分: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..

    }