Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Volley request class for easy parsing of JSON using Gson.
/**
* Copyright 2013 Adam Speakman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package your.pkg.here;
import java.io.UnsupportedEncodingException;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
public class GsonRequest<T> extends JsonRequest<T> {
Class<T> mResponseClass;
public GsonRequest(int method, String url, String requestBody, Class<T> responseClass, Listener<T> listener,
ErrorListener errorListener) {
super(method, url, requestBody, listener, errorListener);
mResponseClass = responseClass;
// Do some generic stuff in here - for example, set your retry policy to
// longer if you know all your requests are going to take > 2.5 seconds
// etc etc...
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String jsonString = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
T response = new GsonBuilder().create().fromJson(jsonString, mResponseClass);
com.android.volley.Response<T> result = com.android.volley.Response.success(response,
HttpHeaderParser.parseCacheHeaders(networkResponse));
return result;
} catch (UnsupportedEncodingException e) {
return com.android.volley.Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return com.android.volley.Response.error(new ParseError(e));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment