2017-09-13 3 views
0

APIを残してログイン詳細を送信するアンドロイドアプリを開発しています。これから応答があります。サーバからのログイン情報にエラーがあることを確認したいと思います。アンドロイドの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)

のエラーこれは、あなたがすでにパラメータとしてJSONObjectを受け

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

}

+0

まさにあなたは私が得るとき、私は私の質問でJSONレスポンスを言及したが、ユーザーの資格情報が正しくないエラー{ 「タイムスタンプ」である場合、これが応答である –

+0

@NavneetKrishnaを取得し、エラーが何でありますか: 1505309139410、 "ステータス":401、 "エラー": "不正"、 "例外": "org.springframework.security.authentication.BadCredentialsException"、 "メッセージ": "不正"、 "パス": "/auth " } –

答えて

0

私の成功応答です。

あなたのコードは次のようにする必要があります:私はあなたのJSONで "トークン" を参照してくださいすることはできません

 @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でトークンを受け取るようにしてください。

はそれが役に立てば幸い:)

+0

次のエラーが表示されます –

+0

どのようなエラー????? –

+0

私は質問を編集する質問 –

関連する問題