【问题标题】:Android. Volley. volleyError.networkResponse is null安卓。凌空抽射。 volleyError.networkResponse 为空
【发布时间】:2015-10-30 00:23:31
【问题描述】:

我正在使用 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 null? volleyError.networkResponse.data 应包含字符串 {"error":"User is not activated"}。为什么这没有发生?有什么想法吗?

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

【问题讨论】:

标签: android android-volley


【解决方案1】:

所以我已经在 4.3 之前的设备上对其进行了测试,但在 4.3 之后的设备上似乎它不起作用。确实如此。

我已经要求后端人员将错误响应从 401 更改为 403,现在看来它可以工作了。我在某处读到 401 的标头可能是错误的或可能不包含适当的数据,这会使代码变得混乱。

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

【讨论】:

    【解决方案2】:

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

    1. Internet 连接是否处于活动状态?
    2. 您尝试连接的网址是否正确?
    3. 在服务器端,检查您是否允许移动连接?在 Web 端有一个 Web 服务配置,仅允许来自移动设备或 Web 浏览器或两者的连接。我认为这可能是您的问题。

    【讨论】:

    • 其他调用有效。因此,如果调用返回 200 代码,则一切正常。它去了它应该去的地方,没关系。
    • @AndreiBogdan 尝试检查您在设备端从服务器端收到的所有响应(我的意思是响应格式。如果是 JSON,请尝试验证来自 codebeautify.org/jsonviewer 的响应)。
    • 这就是问题所在。我已经在运行 5.0 的设备上对其进行了测试,并且运行良好。在 4.2.2 上。它没有。哦,来吧!!!!
    【解决方案3】:

    有时,您的 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;
    }
    

    标头应与后端设置的内容正确对应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2018-05-01
      • 2016-01-22
      • 2023-03-29
      • 2016-12-27
      相关资源
      最近更新 更多