Android。排球。 volleyError.networkResponse 为空

Android. Volley. volleyError.networkResponse is null

我正在使用 Volley 调用网络服务 所以...我正在打这个电话:

POST /api/user/login HTTP/1.1
Content-Type: application/json
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.2.2; Samsung Galaxy S3 - 4.2.2 - API 17 - 720x1280 Build/JDQ39E)
Host: /*No need to show this here.*/
Connection: Keep-Alive
Accept-Encoding: gzip
Content-Length: 43

{"password":"sg","email":"string1@str.com"}

我收到了这个回复(注意 401 错误):

HTTP/1.1 401 Unauthorized
Server: nginx/1.6.3
Date: Thu, 06 Aug 2015 17:39:15 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.27

{"error":"User is not activated"}

我的请求对象如下所示:

public class CustomJsonObjectRequest extends JsonRequest<JSONObject> {

    private Class responseType = null;

    public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<org.json.JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse resp) {
        return null; //For the purpose of this question, let's presume it's null. It doesn't even enter here, so no use considering what's in here
    }

    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError) {

        /** IF  I SET A BREAKPOINT ANYWHERE IN HERE ... volleyError.networkResponse WILL BE NULL !!! WHY ?! You can ignore what's below ... it's not really important*/

        JSONObject jsonResp = null;
        String message = "Could not connect to the server.";// default response
        if (volleyError != null && volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
            try {
                /* get the object */
                jsonResp = new JSONObject(new String(volleyError.networkResponse.data));
                message = jsonResp.getString("detail");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            UDebug.log("PARSED NETWORK ERROR: [" + new VolleyError(message) + "]");
            return new VolleyError(message);
        }
        return new VolleyError(message);
    }

}

它应该输入 parseNetworkError,但为什么 volleyError.networkResponse 是空的? volleyError.networkResponse.data 应包含字符串 {"error":"User is not activated"}。为什么这没有发生?有什么想法吗?

如果您需要一些额外的代码或说明,请告诉我...

在这种情况下:检查以下内容

  1. 互联网连接是否有效?
  2. 您尝试连接的 url 是否正确?
  3. 在服务器端,检查您是否允许移动连接? Web 端有一个 Web 服务配置,仅允许来自移动设备或 Web 浏览器或两者的连接。我想这可能是你的问题。

所以我在 4.3 之前的设备上测试过它,似乎它不起作用,在 4.3 之后的设备上。确实如此。

我已经要求后端人员将错误响应从 401 更改为 403,现在看来可以使用了。我在某处读到 401 的 header 可能是错误的或者可能不包含适当的数据并且它使代码 co berzerk.

我不知道具体的原因。我们将其保留为 403 错误,一切正常。

有时,空响应可能是由您的 getHeaders() 方法中的内容引起的。

@Override
    public Map<String, String> getHeaders() {
        HashMap<String, String> headers = new HashMap<>();
            //headers.put("Content-Type", "application/json");
            //headers.put(VolleyUtils.KEY_API_KEY, "...............");
            
            return headers;
}

headers 应与 back-end 中的设置正确对应。