gpt4 book ai didi

android - 响应代码 304 和 200 时的 Volley 异常错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:31 26 4
gpt4 key购买 nike

在使用 Volley 库时,我注意到在进行 POST 时 JsonObjectRequest , 如果服务器返回代码 304 或 200 而响应中没有数据 (response.data) , Volley 将其解释为错误响应,而不是成功。

我设法通过在方法 Response<JSONObject> parseNetworkResponse(NetworkResponse response) 中添加几行代码来解决它在类里面JsonObjectRequest.java .

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
if (!response.notModified) {// Added for 304 response
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} else // Added for 304 response
return Response.success(new JSONObject(),HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
Log.v("Volley", "UnsupportedEncodingException " + response.statusCode);
if (response.statusCode == 200)// Added for 200 response
return Response.success(new JSONObject(), HttpHeaderParser.parseCacheHeaders(response));
else
return Response.error(new ParseError(e));
} catch (JSONException je) {
Log.v("Volley", "JSONException " + response.statusCode);
if (response.statusCode == 200)// Added for 200 response
return Response.success(new JSONObject(),HttpHeaderParser.parseCacheHeaders(response));
else
return Response.error(new ParseError(je));
}
}

它是这个问题的最佳解决方案吗?

谢谢!

编辑

检查类 BasicNetwork.java我意识到 Volley 通过询问是否 httpResponse.getEntity() != null 来检查响应是否没有数据.

// 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];
}

但问题仍然是当 Volley 尝试使用 response.data == new byte[0] 创建新字符串时出现的 JSONException在 parseNetworkResponse 方法中。

最佳答案

Miguel- 是否仅当响应成功时才调用此方法?

对于所有状态代码 <200 或状态代码 >200 的 volley 调用 parseNetworkError(VolleyError volleyError) 而不是 parseNetworkResponse(NetworkResponse response) 方法。看这里-

https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/toolbox/BasicNetwork.java

行号-118-120

  if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}

和对应的catch block 行号——128 -151

catch (IOException e) {
int statusCode = 0;
NetworkResponse networkResponse = null;
if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
} else {
throw new NoConnectionError(e);
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents,
responseHeaders, false);
if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
statusCode == HttpStatus.SC_FORBIDDEN) {
attemptRetryOnException("auth",
request, new AuthFailureError(networkResponse));
} else {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(networkResponse);
}
}

如果您想覆盖此行为,您可以在 BasicNetwork.java->performRequest 方法中添加您的状态代码特定实现。

编辑:所以这不是因为状态代码,而是因为空响应。好吧,我认为您正在做正确的事情来实现您的自定义 Request 类。Volley 带有一些预定义的流行请求类型以便于使用,但您始终可以创建自己的请求。我宁愿在反序列化之前简单地检查以下字符串是否为空,而不是基于状态代码的实现 -

String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
if (!jsonString .isEmpty()) {
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
}
else {
return Response.success(new JSONObject(),
HttpHeaderParser.parseCacheHeaders(response));
}

**还没有测试过这个,但你明白了:)

关于android - 响应代码 304 和 200 时的 Volley 异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21953519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com