Android:Catch Result但在LogCat中显示json值

时间:2016-07-29 03:30:31

标签: java android json

您好我正在创建一个具有登录功能并且值在API中的应用 但是,当我尝试使用正确的用户名和密码登录时,jsonObject会显示在我的LogCat中但不会显示在我的应用中,结果就是Catch。

    StringRequest strReq = new StringRequest(Request.Method.POST,
    AppConfig.URL_LOGIN, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Login Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                // Check for error node in json
                if (!error) {
                    // user successfully logged in
                    // Create login session
                    session.setLogin(true);

                    // Now store the user in SQLite
                    String uid = jObj.getString("uid");

                    JSONObject user = jObj.getJSONObject("user");
                    String email = user.getString("email");

                    // Inserting row in users table
                    db.addUser(email, uid);

                    // Launch main activity
                    Intent intent = new Intent(MainActivity.this,
                            Home.class);
                    startActivity(intent);
                    finish();
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            "WRONG!!!!!!", Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("email", email);
            params.put("password", password);

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
 }


}

异常的Stacktrace:

Login Response: {"id":1,"name":"PB Admin","fname":"PB","lname":"Admin","email":"admin@admin.com","access_lvl":1,"created_at":"2016-07-21
20:46:53","updated_at":"2016-07-21 12:46:53"} 07-29 11:24:23.756
10739-10739/com.purplebug.gem.loginregistertest W/System.err:
org.json.JSONException: No value for error 07-29 11:24:23.766
10739-10739/com.purplebug.gem.loginregistertest W/System.err:     at
org.json.JSONObject.get(JSONObject.java:389) 07-29 11:24:23.766
10739-10739/com.purplebug.gem.loginregistertest W/System.err:     at
org.json.JSONObject.getBoolean(JSONObject.java:410) 07-29 11:24:23.766
10739-10739/com.purplebug.gem.loginregistertest W/System.err:     at

com.purplebug.gem.loginregistertest.MainActivity$2.onResponse(MainActivity.java:105)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at
com.purplebug.gem.loginregistertest.MainActivity$2.onResponse(MainActivity.java:96)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at
com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at
com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at
com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at
android.os.Handler.handleCallback(Handler.java:739) 07-29 11:24:23.766
10739-10739/com.purplebug.gem.loginregistertest W/System.err:     at
android.os.Handler.dispatchMessage(Handler.java:95) 07-29 11:24:23.766
10739-10739/com.purplebug.gem.loginregistertest W/System.err:     at
android.os.Looper.loop(Looper.java:145) 07-29 11:24:23.766
10739-10739/com.purplebug.gem.loginregistertest W/System.err:     at
android.app.ActivityThread.main(ActivityThread.java:5951) 07-29
11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
07-29 11:24:23.766 10739-10739/com.purplebug.gem.loginregistertest
W/System.err:     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 07-29
11:24:23.786 10739-10739/com.purplebug.gem.loginregistertest D/Volley:
[1] Request.finish: 7348 ms: [ ] http://sanofi.purplebug.net/api/login
0x5e4dbc8b NORMAL 1

3 个答案:

答案 0 :(得分:1)

原因是

  

没有错误值

因为您的response json

{  
   "id":1,
   "name":"PB Admin",
   "fname":"PB",
   "lname":"Admin",
   "email":"admin@admin.com",
   "access_lvl":1,
   "created_at":"2016-07-21 20:46:53",
   "updated_at":"2016-07-21 12:46:53"
}

并且它不会包含error字段,因此当您尝试获取json(boolean error = jObj.getBoolean("error");)中不存在的值时,您的代码将抛出{{ 1}}

因此,只有在json中存在错误时才会出错

JSONException

或者,您可以在boolean error = false; if (jObj.has("error")) { error = jObj.getBoolean("error"); }

中编辑允许回复error字段的服务器代码

答案 1 :(得分:0)

在您的日志中,明确提到键错误没有值。

Login Response: {
"id":1,
"name":"PB Admin",
"fname":"PB",
"lname":"Admin",
"email":"admin@admin.com",
"access_lvl":1,
"created_at":"2016-07-21 20:46:53",
"updated_at":"2016-07-21 12:46:53"
}



07-29 11:24:23.756 10739-10739/com.purplebug.gem.loginregistertest  
W/System.err: org.json.JSONException: No value for error 

要解决此问题,请在JSON响应中添加带有值的错误键。或者,您可以使用jsonObject.optBoolean("error") 代替 jsonObject.getBoolean("error")。如果没有键,它将设置默认值boolean,它是 false 但从不抛出异常。

答案 2 :(得分:0)

首先

您已在LogCat打印回复,因此您获得的结果是合乎逻辑的。不是吗?

Log.d(TAG, "Login Response: " + response.toString());

然后按

@Phan Van Linh 's Answer

我希望你的问题能够得到解决.. 祝你好运......