With the Volley libraries, I extended the Request object to implement GSON serialization. I then extended that new object for how I want to do some of my PUT
requests. This is the first object for the GSON serialization:
@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));
}
}
When my network response gets to Response <t> returnMessage = Response.success(parsedGSON, HttpHeaderParser.parseIgnoreCacheHeaders(response));
I have populated <t>
objects with the correct classes I passed in completely serialized with all variables and no errors. Yet for some reason Volley jumps to } catch (JsonSyntaxException je) {
and I can't reveal the contents of je
with debugging breakpoints or printing logs. Also in my extended class:
new ErrorListener() {
@SuppressWarnings("unused")
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
The onErrorResponse
is never called (neither is my onResponse
section either)
So now I have no idea why Volley is catching a JSONException, when serialization was successful, and I have no idea why Volley isn't returning the Error objects
Insight appreciated
The answer is that any Volley function you extend or override must have
@Override
protected void deliverResponse(T response) {
// TODO Auto-generated method stub
mListener.onResponse(response);
}
function implemented. The Listener must be initialized in the constructor and have the onResponse method implemented.
Otherwise your network call will never return in the onResponse
section.
EDIT: and your extended Request class has to also implement deliverError, along with deliverResponse
private final Listener<T> mListener;
private ErrorListener mErrorListener;
@Override
public void deliverError(VolleyError error) {
mErrorListener.onErrorResponse(error);
}
with ErrorListener
initialized in your constructor