Android Volley库无法与204一起使用并且出现空响应 [英] Android Volley library not working with 204 and empty body response

查看:88
本文介绍了Android Volley库无法与204一起使用并且出现空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的Volley库,并且当我的api返回响应中没有正文的204时遇到问题.看来BasicNetwork.java中的以下代码无法正常工作:

I'm using the latest Volley library and I'm having issues when my api is returning a 204 with no body in the response. It seems that the following code in BasicNetwork.java isn't working as expected:

// Some responses such as 204s do not have content.  We must check.
if (httpResponse.getEntity() != null) {
    responseContents = entityToBytes(httpResponse.getEntity());
} else {
    // Add 0 byte response as a way of honestly representing a
    // no-content request.
    responseContents = new byte[0];
}

对于我来说,getEntity的结果永远不会为null,但它为空.我已经通过检查curl和postman确保了我的API没有返回任何东西(只是为了确保我不会发疯).还有其他人有这样的问题吗?

the result from getEntity is never null for me, but it is empty. I've made sure that my API is returning nothing by checking curl and postman (just to be extra sure i'm not going crazy). Has anyone else has such an issue?

现在我将其更改为:

if (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)

我知道这并不能解决根本原因,但是我想确保在深入探讨此问题之前,我不会错过任何明显的内容.

which I know isn't solving the root cause, but I want to make sure that I'm not missing anything obvious before diving deeper into this problem.

谢谢!

抱歉,我忘了提到实际的问题是,在EntityToBytes方法中发生超时异常,这很奇怪,因为没有要检索的正文.

Sorry, I forgot to mention that the actual problem was that a timeout exception occurs whilst in the method entityToBytes, which is strange since there is no body to retrieve.

此外,我还没有使用真正的功能齐全的Web服务API,因为它尚不可用.取而代之的是,我正在蜂房上连接到模拟的Web服务,但我看不出蜂房可能是问题所在.

Also, i'm not using an actual fully functioning webservice API, as it's not available yet. Instead i'm connecting to a mocked out webservice on apiary, but i don't see how apiary could be the problem.

推荐答案

这不是对解决方案的解答,而是对解决方案的详细说明!首先,我使用了来自我的API的204个响应,并且遇到了与您完全相同的问题.我在BasicNetwork.java中使用了您的代码来解决它-if (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)

This is less an answer than an elaboration on your solution! First off, I use 204 responses from my API and had the exact same issue you had. I used your code in BasicNetwork.java to solve it - the line if (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)

我还发现,如果我使用标准的JsonObjectRequest请求,则会触发Response.ErrorListener,因为正文为空.

What I also found was that if I use a standard JsonObjectRequest request then the Response.ErrorListener would be triggered because the body was null.

我创建了一个新的JsonObjectRequestWithNull,如果正文为空或为空,它会提供成功的响应.代码:

I created a new JsonObjectRequestWithNull which provides a success response in the event of a null or blank body. Code:

public class JsonObjectRequestWithNull extends JsonRequest<JSONObject> {

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

public JsonObjectRequestWithNull(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener,
                         Response.ErrorListener errorListener) {
    this(jsonRequest == null ? Request.Method.GET : Request.Method.POST, url, jsonRequest,
            listener, errorListener);
}

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        //Allow null
        if (jsonString == null || jsonString.length() == 0) {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

}

相关的位是:

        //Allow null
        if (jsonString == null || jsonString.length() == 0) {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }

希望对某人有帮助.

这篇关于Android Volley库无法与204一起使用并且出现空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆