Android Volley 库不适用于 204 和空体响应

标签 android android-volley http-status-code-204

我正在使用最新的 Volley 库,当我的 api 返回 204 而响应中没有正文时,我遇到了问题。 BasicNetwork.java 中的以下代码似乎没有按预期工作:

// 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 的结果对我来说永远不会为空,但它是空的。我已经通过检查 curl 和 postman 确保我的 API 没有返回任何内容(只是为了额外确保我不会发疯)。有没有其他人有这样的问题?

现在我只是将 if 更改为:

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

我知道这并不能解决根本原因,但我想确保在深入研究这个问题之前我没有遗漏任何明显的东西。

谢谢!

编辑: 抱歉,我忘了说,实际问题是在 entityToBytes 方法中发生超时异常,这很奇怪,因为没有要检索的主体。

此外,我没有使用实际功能齐全的网络服务 API,因为它尚不可用。相反,我正在连接到养蜂场上模拟的网络服务,但我不明白养蜂场怎么可能是问题所在。

最佳答案

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

我还发现,如果我使用标准的 JsonObjectRequest 请求,那么 Response.ErrorListener 将被触发,因为正文为 null。

我创建了一个新的 JsonObjectRequestWithNull,它在出现 null 或空白正文时提供成功响应。代码:

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 和空体响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392813/

相关文章:

android - 在 Volley 中设置 cookie 不起作用

Android volley 错误 : "Trust anchor for certification path not found", 仅在真实设备中,而不是模拟器

javascript - 带有 204 响应和回调函数的信标跟踪图像

javascript - 如何正确检测 iframe 加载响应 204 No Content?

android - 使用 ZXing 时 Xperia 设备上的 RuntimeException

android - 无法将我的位置叠加层添加到 osmdroid map

android-volley - 为什么我的recyclerview 只显示第一项的内容?

python - Flask test_client 无法处理 HTTP 204 无数据

java - 在 Android 中,有没有使用类来格式化一组 View 的方法?

java - 如何保存 OkHttp Client 对象?