玩弄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 …Run Code Online (Sandbox Code Playgroud) android request http-status-code-304 android-volley jsonobject