VolleyPlus : NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException

前端 未结 4 738
春和景丽
春和景丽 2021-01-18 01:09

I am trying to run below code:

Map requestMap = new HashMap<>();
Long unixTime = System.currentTimeMillis() / LONG_1000;
requestM         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 02:11

    You need to override the parseNetworkResponse method in StringRequest and fix a vulnerability which does not anticipate a null header map:

    StringRequest stringRequest =  new StringRequest(Request.Method.GET, url, new Response.Listener() {
        @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 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.emptyMap(), // this is the important line, set an empty but non-null map.
                                   response.notModified,
                                   response.networkTimeMs);
    
    
            }
    
            return super.parseNetworkResponse(response);
        }
    };
    

    Hope this helps.

提交回复
热议问题