使用 Volley 库,我扩展了 Request 对象以实现 GSON 序列化。然后,我扩展了该新对象以实现我的一些PUT
请求。这是 GSON 序列化的第一个对象:
@Override
protected Response<t> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); //response may be too large for string?
t parsedGSON = mGson.fromJson(jsonString, cls);
Response <t> returnMessage = Response.success(parsedGSON,
HttpHeaderParser.parseIgnoreCacheHeaders(response));
return returnMessage;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
} catch (JsonSyntaxException je) {
je.printStackTrace();
Log.e("GsonRequest", je.getMessage()!=null?je.getMessage():"JsonSyntaxError");
return Response.error(new ParseError(je));
}
}
当我的网络响应到达时,Response <t> returnMessage = Response.success(parsedGSON, HttpHeaderParser.parseIgnoreCacheHeaders(response));
我已经使用我传入的正确类填充<t>
了对象,这些对象完全序列化了所有变量并且没有错误。然而由于某种原因,Volley 跳转到} catch (JsonSyntaxException je) {
,我无法je
通过调试断点或打印日志来揭示内容。同样在我的扩展课程中:
new ErrorListener() {
@SuppressWarnings("unused")
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
onErrorResponse
永远不会被调用(我的部分也不是onResponse
)
所以现在我不知道为什么 Volley 在序列化成功时会捕获 JSONException,我也不知道为什么 Volley 不返回错误对象
洞察力赞赏