使用Volley处理JSONRequest中的空响应 [英] Handle an empty response in a JSONRequest with Volley

查看:71
本文介绍了使用Volley处理JSONRequest中的空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用Volley发出POST请求,对于我来说,一个很好的响应是带有空主体的201.我正在使用JSONRequest拨打电话.

I'm using Volley to make a POST request in my application, and in my case, a good response is a 201 with an empty body. I'm using a JSONRequest, to make the call.

我的问题是由于响应为空,因此调用了错误响应处理程序.

My problem is that the error response handler is getting called because response is empty.

以下是我的要求:

    Request request = new JsonRequest<Object>(Request.Method.POST, url, body, new Response.Listener<Object>() {

        @Override
        public void onResponse(Object response) {

        }
    }, new ErrorListener(context)) {

        @Override
        protected Response<Object> parseNetworkResponse(NetworkResponse response) {

            Log.d(TAG, "success!!!!!!");
            if (response.statusCode == 201)
                mListener.resetPasswordWasSent();
            return null;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/json");
            params.put("Accept", "application/json");
            return params;
        }
    };

    requestQueue.add(request);

我的parseNetworkResponse函数被调用,然后ErrorListeneronResponse方法从不被命中,因为我在ErrorListener中得到了NullPointerException.

My parseNetworkResponse function is getting called, then the ErrorListener, and the onResponse method never gets hits because I get a NullPointerException in the ErrorListener.

我可以在错误侦听器中忽略NullPointerException,但我不想这样做.显然,我可以简单地在parseNetworkResponse中发送回调,但是我不想弹出任何错误.

I can ignore the NullPointerException in my error listener, but I'd prefer not to. Obviously, I can simply send my callback in the parseNetworkResponse, but I don't want to have any errors popping up.

有人知道我该怎么处理吗?

Anyone know how I should handle this?

这是堆栈跟踪:

05-06 09:44:19.586  27546-27560/com.threepoundhealth.euco E/Volley﹕ [1830] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
    java.lang.NullPointerException
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:126)

推荐答案

您可以尝试像这样黑客入侵.创建一个JsonObjectRequest子类,重写parseNetworkResponse方法并检查响应数据(如果它为空的byte[]), 将数据替换为空json {}byte[]表示形式.

You could try to hack like this. Create a JsonObjectRequest subclass, override the parseNetworkResponse method and check the response data, if it is an empty byte[], replace the data with byte[] representation of a empty json {}.

public class VolleyJsonRequest extends JsonObjectRequest {

    ...

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            if (response.data.length == 0) {
                byte[] responseData = "{}".getBytes("UTF8");
                response = new NetworkResponse(response.statusCode, responseData, response.headers, response.notModified);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return super.parseNetworkResponse(response);
    }    
}

这篇关于使用Volley处理JSONRequest中的空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆