Skip to content

Instantly share code, notes, and snippets.

@vedant1811
Created February 16, 2015 14:25
Show Gist options
  • Save vedant1811/2e15e7215b60b6f04051 to your computer and use it in GitHub Desktop.
Save vedant1811/2e15e7215b60b6f04051 to your computer and use it in GitHub Desktop.
JsonArrayRequest based on volley
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
/**
* Created by vedant on 1/10/14.
*/
public class JsonArrayRequest extends JsonRequest<JSONArray> {
/**
* Creates a new request.
* @param url URL to fetch the JSON from
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/
public JsonArrayRequest(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
super(Method.GET, url, null, listener, errorListener);
}
/**
* Creates a new request.
* @param url URL to fetch the JSON from
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/
public JsonArrayRequest(String url, JSONObject postData, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
super(
(postData == null) ? Method.GET : Method.POST, url,
(postData == null) ? null : postData.toString(),
listener,
errorListener
);
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString =
new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment