Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android截击库无法使用204和空体响应_Android_Android Volley_Http Status Code 204 - Fatal编程技术网

Android截击库无法使用204和空体响应

Android截击库无法使用204和空体响应,android,android-volley,http-status-code-204,Android,Android Volley,Http Status Code 204,我使用的是最新的截击库,当我的api返回204而响应中没有正文时,我遇到了问题。BasicNetwork.java中的以下代码似乎没有按预期工作: // Some responses such as 204s do not have content. We must check. if (httpResponse.getEntity() != null) { responseContents = entityToBytes(httpResponse.getEntity()); } el

我使用的是最新的截击库,当我的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 (statusCode != HttpStatus.SC_NO_CONTENT && httpResponse.getEntity() != null)
我知道这并不能解决问题的根本原因,但在深入研究这个问题之前,我想确保我没有遗漏任何明显的东西

谢谢

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


另外,我没有使用真正的功能齐全的webservice API,因为它还不可用。相反,我正在连接到Apariy上的模拟Web服务,但我不认为Apariy会成为问题。

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

我还发现,如果使用标准的
JsonObjectRequest
请求,则会触发
响应.ErrorListener
,因为主体为空

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

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));
    }
}

希望对其他人有所帮助。

只要您使用的是标准的Volley库,而没有任何自定义的HttpStack或对其进行任何修改(gradle dependency:compile'com.mcxiaoke.Volley:library:1.0.15'),那么您需要包括的唯一更改就是请求类中的parseNetworkResponse方法(假设您使用Gson转换为Json,这来自我编写的自定义GsonRequest类)

@覆盖
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
final String json=新字符串(
response.data,HttpHeaderParser.parseCharset(response.headers));
if(TextUtils.isEmpty(json)){
返回Response.success(null,HttpHeaderParser.parseCacheHeaders(Response));
}
返回Response.success(gson.fromJson(json,clazz),null);
}捕获(不支持的编码异常e){
返回Response.error(新的ParseError(e));
}捕获(JsonSyntaxException e){
返回Response.error(新的ParseError(e));
}
}

entityToBytes
在没有内容时似乎返回一个0长度的字节[],很抱歉,这太晚了,但我也遇到了同样的问题,真正的问题是在我的HttpStack实现中。我正在使用一个较新的Apache库,并在新对象和通知对象之间进行转换,如果
getEntity()
was null我返回了一个空白实体。您的问题可能与此类似。请查看您的
performRequest
实现
HttpStack
的方法,如果它的响应
getEntity
返回
null
,那么截击应该可以正常工作。如果像我一样,您正在新旧apache库或doi之间转换在其他处理中,您可能添加了一个空实体。204因为web服务中的函数返回“void”,在我看来,您不需要处理响应数据。我花了3天时间讨论这个问题,您解决了我的问题!我爱您兄弟
        //Allow null
        if (jsonString == null || jsonString.length() == 0) {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        final String json = new String(
                response.data, HttpHeaderParser.parseCharset(response.headers));

        if (TextUtils.isEmpty(json)) {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }

        return Response.success(gson.fromJson(json, clazz), null);
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    }
}