VolleyPlus:NetworkDispatcher.run:未处理的异常java.lang.NullPointerException

时间:2015-10-08 13:53:15

标签: android httprequest

我正在尝试运行以下代码:

Map<String, Object> requestMap = new HashMap<>();
Long unixTime = System.currentTimeMillis() / LONG_1000;
requestMap.put(KEY_TIME, unixTime);
JWTSigner signer = new JWTSigner(SECRET);
String token = signer.sign(requestMap);
String url = BASE_URI + "/Data/Categories?d=" + token;
StringRequest stringRequest =  new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d(TAG, response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, error.getLocalizedMessage());
    }
});

RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
mRequestQueue.add(stringRequest);

但我收到以下错误:

[6056] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference
    at com.android.volley.toolbox.HttpHeaderParser.parseCharset(HttpHeaderParser.java:243)
    at com.android.volley.toolbox.HttpHeaderParser.parseCharset(HttpHeaderParser.java:262)
    at com.android.volley.request.StringRequest.parseNetworkResponse(StringRequest.java:70)
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:133)
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference

我不知道问题是什么。有人可以帮忙吗?

提前致谢。

3 个答案:

答案 0 :(得分:8)

您需要覆盖parseNetworkResponse中的StringRequest方法并修复一个不会出现空标题地图的漏洞:

StringRequest stringRequest =  new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d(TAG, response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, error.getLocalizedMessage());
    }
}) {
    @Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
        if (response.headers == null)
        {
            // cant just set a new empty map because the member is final.
            response = new NetworkResponse(
                               response.statusCode,
                               response.data,
                               Collections.<String, String>emptyMap(), // this is the important line, set an empty but non-null map.
                               response.notModified,
                               response.networkTimeMs);


        }

        return super.parseNetworkResponse(response);
    }
};

希望这有帮助。

答案 1 :(得分:1)

我跟踪了堆栈跟踪,我认为由于某种原因,方法response.headers中的根本原因parseNetworkResponse为空。我建议以下内容来证实这一点。

  1. 创建一个扩展StringRequest
  2. 的类
  3. 在此类中覆盖&#39; parseNetworkResponse()`。它应该如下所示:

       protected Response<String> parseNetworkResponse(NetworkResponse response) {
              Log.d("Response Header = " + response.headers);
              super.parseNetworkResponse(response);
       }
    
  4. 使用此类而不是StringRequest

  5. 完成后,查看标头是否为空。这应该缩小问题的范围。顺便说一句,你使用的是Volley的哪个版本。我从未遇到过这样的问题。

答案 2 :(得分:-1)

我在build.gradle.dependencies上写了这个。

compile 'com.android.volley:volley:1.0.0'

它有效。