2017-06-04 7 views
0

JSONExceptionはリクエストが失敗したときにのみ有効ですが、リクエストが有効な場合(有効なユーザー名、パスワード)、別のアクティビティにリダイレクトされますが、代わりにJSONExceptionが表示されます。JSONExceptionが成功しました

私は別のアクティビティにリダイレクトするのではなく、サーバーから受信したJSON文字列を表示します。

この私の onResponse機能です

 @Override 
     public void onResponse(String response){ 
      try { 
       JSONObject volleyResponse = new JSONObject(response); 

       boolean success  = volleyResponse.getBoolean("success"); 
       String message  = volleyResponse.getString("message"); 

       String UUID  = volleyResponse.getString("unique_user_id"); 
       String LLOGIN = volleyResponse.getString("last_login"); 
       String RDATE = volleyResponse.getString("registration_date"); 
       String MONEY = volleyResponse.getString("money"); 

       if(success){ 
        Intent intent = new Intent(Authentication.this, Mainpage.class); 
        intent.putExtra(KEY_USERNAME, strUsername); 
        intent.putExtra(KEY_UUID, UUID); 
        intent.putExtra(KEY_LLOGIN, LLOGIN); 
        intent.putExtra(KEY_RDATE, RDATE); 
        intent.putExtra(KEY_MONEY, MONEY); 
        startActivity(intent); 
       } 
      } catch(JSONException e) { 
       response = response.replace("\"", ""); 
       response = response.replace("status:false,message:", ""); 
       response = response.replace("{", ""); 
       response = response.replace("}", ""); 
       messageText.setText(response); 
      } 
     } 

JSONレスポンスそれが成功する場合:

{"unique_user_id":"4e99a28a-0cb2-30a9-ac51-ccd4629bcef1","last_name":"therealaxis","password":"$2a$10$9qRjW\/vJreCQg3u5dO6eW.8PhZBTpGaPNK5qRIYP.XTx2PVY1yrOi","last_login":"1 week ago","registration_date":"1 week ago","money":"100.00","success":true} 
+0

あなたの応答にメッセージ文字列がないため、JSONExceptionがスローされます – petul

+0

失敗した場合はメッセージ文字列がありますが、成功した場合は統計情報と真の[真偽値]成功キーが表示されます。 –

+0

はい、ただし、常にメッセージ属性にアクセスしようとします。それが成功したとしても。その時点で、それはあなたのキャッチブロックに直接ジャンプします – petul

答えて

0

あなたのJSONレスポンスはとてもJSONExceptionがスローされ、何のメッセージ文字列を持っていません。存在する場合にのみメッセージ属性にアクセスしたい場合は、アクセスする前にJSONObject.hasを使用してください。

関連する問題