JSONException on success



我认为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}

您的JSON响应没有消息字符串,因此会抛出JSoneXception。如果您只想访问消息属性,以防万一,请在访问它之前使用jsonobject.has。

最新更新