com.android.volley.NoConnectionError:java.net.ProtocolException:意外的状态行:

时间:2016-07-11 06:57:15

标签: android android-volley

我正在使用凌空库来获取响应,大部分时间它在某些情况下工作正常我收到上述错误,当我尝试在浏览器中检查它工作正常。请提出解决这个问题的建议。我正在尝试删除缓存并检查甚至 - 虽然问题仍然存在但我不明白我在哪里做错了。

我收到以下错误

ava.lang.NullPointerException: Attempt to read from field 'byte[] com.android.volley.NetworkResponse.data' on a null object reference
                                                  at com.pcnc.bop.helpers.VolleyRequest$2.onErrorResponse(VolleyRequest.java:98)
                                                  at com.android.volley.Request.deliverError(Request.java:577)
                                                  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                  at android.os.Looper.loop(Looper.java:135)
                                                  at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

这是我的截击请求代码段

  public void requestString(final String requestName,
                              final String webserviceUrl,
                              final Map<Object, Object> requestParams, final int webMethod,
                              final boolean getCache) {
        LogUtils.i("Sending Request", webserviceUrl);
        StringRequest stringRequest = new StringRequest(webMethod,
                webserviceUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                LogUtils.i("Response", response);
                mRequestCompletedListener.onRequestCompleted(
                        requestName, true, response, null);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                String errorResponse = null;

                if (getCache) {
                    final Cache cache = AppController.getInstance()
                            .getRequestQueue().getCache();
                    final Entry entry = cache.get(webserviceUrl);
                    if (entry.data != null) {
                        try {
                            errorResponse = new String(entry.data, "UTF-8");
                            mRequestCompletedListener
                                    .onRequestCompleted(requestName,
                                            true, errorResponse, null);
                            return;
                        } catch (UnsupportedEncodingException e) {
                            LogUtils.e(TAG, e);
                        }
                    } else {
                        LogUtils.e(TAG, requestName
                                + " Cache does not exist");
                    }
                }
                try {
                    VolleyError responseError = new VolleyError(
                            new String(error.networkResponse.data));
                    LogUtils.i("ErrorResponse", responseError.getMessage());
                    try {
                        if (responseError != null) {
                            final JSONObject responseJson = new JSONObject(responseError.getMessage());
                            // Show Alert Information
                            errorResponse = responseJson.getString(AppConstants.MESSAGE);
                        }
                    } catch (Exception e) {
                        errorResponse = "Unknown";
                    }
                } catch (Exception e) {
                    LogUtils.e(TAG, e);
                }
                mRequestCompletedListener.onRequestCompleted(
                        requestName, false, null,
                        errorResponse);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                @SuppressWarnings("unchecked")
                final Map<String, String> params = (HashMap<String, String>) requestParams
                        .get(VolleyRequestConstants.HTTP_PARAMS);
                if (params != null) {
                    return params;
                }
                return super.getParams();
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                @SuppressWarnings("unchecked")
                final Map<String, String> headers = (HashMap<String, String>) requestParams
                        .get(VolleyRequestConstants.HTTP_HEADERS);
                if (headers != null) {
                    return headers;
                }
                return super.getHeaders();
            }

            @Override
            public com.android.volley.Request.Priority getPriority() {
                final Priority priority = (com.android.volley.Request.Priority) requestParams
                        .get(VolleyRequestConstants.HTTP_PRIORITY);
                if (priority != null) {
                    return priority;
                }
                return super.getPriority();
            }

            @Override
            public String getBodyContentType() {
                final String contentType = (String) requestParams
                        .get(VolleyRequestConstants.HTTP_CONTENT_TYPE);
                if (contentType != null) {
                    return contentType;
                }
                return super.getBodyContentType();
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                final byte[] body = (byte[]) requestParams
                        .get(VolleyRequestConstants.HTTP_BODY_CONTENT);
                if (body != null) {
                    return body;
                }
                return super.getBody();
            }
        };

    /*    DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(retryPolicy);*/
        stringRequest.setTag(requestName);
        // Adding String request to request queue
        AppController.getInstance().addToRequestQueue(stringRequest);
    }

1 个答案:

答案 0 :(得分:0)

这一行:

@Override
public void onErrorResponse(VolleyError error) {
  //...
  VolleyError responseError = new VolleyError( new String(error.networkResponse.data));
  //..
}

导致您遇到的错误,因为error.networkResponse可以转为null。因此,添加空检查或其他catch块或类似