【问题标题】:Android json parsing error in http responsehttp响应中的Android json解析错误
【发布时间】:2018-10-09 01:40:17
【问题描述】:

我正在使用 volley 在 android 中调用 http 请求,但有时我在收到响应时收到 JSON 解析错误,即使响应始终相同。

void login() throws JSONException {
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = String.format("%s/login", getString(R.string.url));
    JSONObject data = new JSONObject();
    data.put("email", etEmail.getText().toString());
    data.put("password", etPassword.getText().toString());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
        (Request.Method.POST, url, data, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Helper.unblockUI(pb, getWindow());
                try{
                    if(response.has("error")){
                        Toast.makeText(LoginActivity.this, response.getString("error"), Toast.LENGTH_SHORT).show();
                    }else if(response.has("id") && response.has("token")){
                        enterMainActivity();
                    }else{
                        Toast.makeText(LoginActivity.this, "Something is wrong", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e){
                    Helper.unblockUI(pb, getWindow());
                    Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Helper.unblockUI(pb, getWindow());
                Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
    });

    Helper.blockUI(pb, getWindow());
    queue.add(jsonObjectRequest);
}

响应是:

{"error":"Email doesn't exist"}

当响应是这样时它不会出错,因为那是有效的 JSON, 我不知道为什么,但有时响应是这样的:

{"error":"Email doesn't exist"

最后减去一个大括号,所以凌空响应是错误的,因为那不是有效的 JSON。

知道为什么会这样吗?因为有时它得到了有效的 json 有时没有,所以我总是从 api 返回相同的响应,但有时响应在 json 对象的末尾减去一个大括号..

成功时输出: 2018-10-09 21:05:22.717 12545-12545/com.omg_indo.itmsapp D/tag: {"error":"Email doesn't exist"}

出错时的输出: 2018-10-09 21:08:02.019 12545-12545/com.omg_indo.itmsapp D/tag: com.android.volley.ParseError: org.json.JSONException: Unterminated object at character 30 of {"error":"Email doesn't exist"

【问题讨论】:

    标签: java android json android-volley


    【解决方案1】:

    我不确定这是否是您的问题的原因,但您可以尝试使用全局单例请求队列,而不是在您的方法中创建新的本地请求队列。为此,您需要将 RequestQueue 作为成员变量保留在您的 Application 类中。

    class MyApplication extends Application{
    
    
        private static MyApplication sInstance;
        private RequestQueue mRequestQueue;
    
    
        @Override
        public void onCreate() {
            super.onCreate();
            sInstance = this;
        }
    
        public static MyApplication getInstance() {
            return sInstance;
        }
    
    
        public synchronized RequestQueue getRequestQueue() {
            if( mRequestQueue == null )
                mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    
            return mRequestQueue;
        }
    
    
    }
    

    然后你会像这样使用它

    MyApplication.getInstance().getRequestQueue().add( jsonObjectRequest );
    

    这将使请求队列在您的应用程序的整个生命周期内保持不变。

    The documentation 在这一点上有点模棱两可——他们曾说

    一个关键概念是 RequestQueue 必须使用 Application 上下文进行实例化,而不是 Activity 上下文。这可确保 RequestQueue 将在您的应用程序的整个生命周期内持续存在,而不是在每次重新创建 Activity 时(例如,当用户旋转设备时)都重新创建。

    但他们也说

    如果您只需要发出一次性请求并且不想离开线程池,您可以在任何需要的地方创建 RequestQueue 并在您的响应或错误出现后调用 RequestQueue 上的 stop()返回,使用 Volley.newRequestQueue() 方法 [...]

    这是您目前正在做的事情,以及我在多个项目中所做的事情,并且从未遇到任何问题。但值得尝试为您的请求队列迁移到单例模式,看看这是否能解决您的问题。

    【讨论】:

    • 感谢您的回答,我已经尝试过您的方法,但仍然......有时它收到了有效的 JSON 对象,有时它收到了带有减号括号的 JSON 对象,导致解析错误..跨度>
    猜你喜欢
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2019-03-16
    • 1970-01-01
    相关资源
    最近更新 更多