This page covers Volley-Gson integration and
also Volley custom Request. We assume that, you are familiar
with Volley
and Gson.
To make a custom request, you need to perform following operation.
- Extend Request<T> class, where T is the type of parsed response or expected response.
- Implement the abstract methods parseNetworkResponse() and deliverResponse().
parseNetworkResponse:
A response contains parse response of a given
type i.e Gson.
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
if (BuildConfig.IS_DEBUG)
Log.d(TAG, "Response :: " + response.data == null ? null : new String(response.data));
return Response.success(
mGson.fromJson(json, mClass), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException ex) {
return Response.error(new ParseError(ex));
} catch (JsonSyntaxException ex) {
return Response.error(new ParseError(ex));
}
}
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
if (BuildConfig.IS_DEBUG)
Log.d(TAG, "Response :: " + response.data == null ? null : new String(response.data));
return Response.success(
mGson.fromJson(json, mClass), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException ex) {
return Response.error(new ParseError(ex));
} catch (JsonSyntaxException ex) {
return Response.error(new ParseError(ex));
}
}
deliverResponse:
This is callback interface which Volley use to
call back you on the main thread using the Object you returned in
parseNetworkResponse().
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
Complete Code:
public class GSONRequest<T> extends
Request<T> {
private static final String TAG = "GSONRequest";
private final Class<T> mClass;
private final Response.Listener<T> mListener;
private final Gson mGson = new Gson();
/**
* Make a POST request and return a parsed object from JSON.
*
* @param url for request
* @param tClass Gson reflection class object
* @param listener responce listener
* @param errorListener responce error listener
*/
public GSONRequest(String url, Class<T> tClass,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, errorListener);
mClass = tClass;
mListener = listener;
}
@Override
public byte[] getBody() throws AuthFailureError {
return super.getBody();
}
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
if (BuildConfig.IS_DEBUG)
Log.d(TAG, "Response :: " + response.data == null ? null : new String(response.data));
return Response.success(
mGson.fromJson(json, mClass), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException ex) {
return Response.error(new ParseError(ex));
} catch (JsonSyntaxException ex) {
return Response.error(new ParseError(ex));
}
}
}
private static final String TAG = "GSONRequest";
private final Class<T> mClass;
private final Response.Listener<T> mListener;
private final Gson mGson = new Gson();
/**
* Make a POST request and return a parsed object from JSON.
*
* @param url for request
* @param tClass Gson reflection class object
* @param listener responce listener
* @param errorListener responce error listener
*/
public GSONRequest(String url, Class<T> tClass,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, errorListener);
mClass = tClass;
mListener = listener;
}
@Override
public byte[] getBody() throws AuthFailureError {
return super.getBody();
}
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
if (BuildConfig.IS_DEBUG)
Log.d(TAG, "Response :: " + response.data == null ? null : new String(response.data));
return Response.success(
mGson.fromJson(json, mClass), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException ex) {
return Response.error(new ParseError(ex));
} catch (JsonSyntaxException ex) {
return Response.error(new ParseError(ex));
}
}
}
Source Code download
Comments
Post a Comment