我在用截击打电话给一个网络服务
所以…我在打电话:

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":"[email protected]"}

我得到了这个回应(注意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"}。为什么没有发生这种事?有什么想法吗?
如果你需要一些额外的代码或澄清,让我知道…

最佳答案

所以我已经在4.3之前的设备上测试过了,但在4.3之后的设备上,它似乎不起作用。是的。
我已经要求后台人员将错误响应从401更改为403,现在似乎可以工作了。我在某个地方读到401的头可能是错误的,或者可能不包含适当的数据,这使得代码是co-berzerk。
具体原因我不知道。我们留下了403个错误,一切都很好。

10-04 12:28