2017-09-13 23 views
0

我正在開發Android應用程序中,我發送的登錄細節休息API,並從我得到的迴應,我要檢查是否有來自服務器的登錄信息的任何錯誤,因爲服務器給我一個迴應如何檢查Android中的其他API響應錯誤?

{ 
    "timestamp": 1505309139410, 
    "status": 401, 
    "error": "Unauthorized", 
    "exception": "org.springframework.security.authentication.BadCredentialsException", 
    "message": "Unauthorized", 
    "path": "/auth" 
} 

這是我的JSON響應

,這是我的登錄用戶的方法

private void Loginuser(final String email_or_phone, final String user_password){ 

    Log.d(TAG,email_or_phone +" "+ user_password); 
    // Tag used to cancel the request 
    String tag_string_req = "req_login"; 
    pDialog.setMessage("login ..."); 
    showDialog(); 

    JSONObject jobj= new JSONObject(); 

    try { 
     jobj.put("username",email_or_phone); 
     jobj.put("password",user_password); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, 
      AppConfig.URL_LOGIN, jobj, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      Log.d(TAG, response.toString() + "Login responce"); 
      hideDialog(); 
      try { 
       boolean error = response.getBoolean("error"); 
       if(!error) { 
        String token = response.optString("token"); 
        Log.d("Token string", token); 
         Intent home = new Intent(LoginActivity.this, HomeActivity.class); 
         home.putExtra("token",token); 
         startActivity(home); 
         finish(); 
       } //Error in login. Get the error message 
       String errorMsg = response.getString("error"); 
       Toast.makeText(getApplicationContext(),errorMsg, Toast.LENGTH_LONG).show(); 
      }catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d(TAG, "Error: " + error.getMessage()); 
     } 
    }){ 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("Content-Type", "application/json; charset=utf-8"); 
      return headers; 
     } 
    }; 
    // Addinrg request to request queue 
    Log.e("Responce string",jsonObjectRequest.toString()); 
    AppController.getInstance(this).addToRequestQueue(jsonObjectRequest, tag_string_req); 

} 

我收到錯誤JSONObject jobj = new JSONObject(response);此行。

JSON

org.json.JSONException: No value for error 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at org.json.JSONObject.get(JSONObject.java:389) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at org.json.JSONObject.getBoolean(JSONObject.java:410) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at com.wowoni.bikesharing.bicyclesharing.activity.LoginActivity$3.onResponse(LoginActivity.java:330) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at com.wowoni.bikesharing.bicyclesharing.activity.LoginActivity$3.onResponse(LoginActivity.java:324) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at android.os.Looper.loop(Looper.java:148) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7325) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at java.lang.reflect.Method.invoke(Native Method) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 09-13 19:23:19.303 22852-22852/com.wowoni.bikesharing.bicyclesharing W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

的錯誤,這是我的成功響應

{ 
"token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI4NjU1MjUzMTE0IiwiYXVkIjoid2ViIiwiZXhwIjoxNTA1OTgzNTY2LCJpYXQiOjE1MDUzNzg3NjZ9.StHWCEHgR_OqwFy-3wYLbbjq0MWBGb1vmdQLlv8gb-DFSrAdAktnCzX6bq0UibUiXMIXjDiyhTFtn1VtHaqbuA" 

}

+0

究竟是什麼,你得到 –

+0

@NavneetKrishna我提到我的問題JSON響應當我得到不過這是當用戶憑據是不正確的錯誤{ 「時間戳」響應錯誤: 1505309139410, 「狀態」:401, 「錯誤」: 「未授權」, 「異常」: 「org.springframework.security.authentication.BadCredentialsException」, 「消息」: 「未授權」, 「路徑」:「/auth「 } –

回答

0

你已經接收的JSONObject作爲參數。

您的代碼應該是這樣的:

 @Override 
     public void onResponse(JSONObject response) { 
      Log.d(TAG, response.toString() + "Login responce"); 
      hideDialog(); 
      try { 
       String errorMsg = response.getString("error"); 
       Toast.makeText(getApplicationContext(),errorMsg, Toast.LENGTH_LONG).show(); 
       int statusCode = response.getInt("status"); 
       String exception = response.getString("exception"); 
       String message = response.getString("message"); 
       String path = response.getString("path"); 

       // do whatever with this responses. 

      }catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 

我看不到 「令牌」 在你的JSON。確保您將在您的JSON中收到一個令牌。

希望它能幫助:)

+0

我得到了以下錯誤 –

+0

什麼錯誤????? –

+0

我編輯問題見問題 –