Implementing a Custom Request Using Volley Library
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Get an intro to this library here.
Join the DZone community and get the full member experience.
Join For FreeWhat Exactly Volley Library Is:
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster.
If your response is a string, image, or JSON, you probably won't need to implement a custom Request
.
Things You Need to Do:
- Extend the
Request<T>
class, where<T>
represents the type of parsed response the request expects. So if your parsed response is a string, for example, create your custom request by extendingRequest<String>
. - Add Gson library compile dependency to your app level build.gradle
- Create model class as per response.
- Add custom request to request queue of volley.
Example:
This example uses the Gson library.
What Does Gson Do?
Gson is a library for converting Java objects to and from JSON using reflection.
GsonRequest class of Volley:
public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Response.Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
* @param headers Map of request headers
*/
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
Model class object:
- Define Java objects that have the same names as their corresponding JSON keys, pass Gson the class object, and Gson will fill in the fields for you
public class CustomResponse {
private String imageName;
private String name;
private String number;
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
List of model class Objects:
public class CustomResponseList {
private List<CustomResponse> customResponseList;
public List<CustomResponse> getCustomResponseList() {
return customResponseList;
}
public void setCustomResponseList(List<CustomResponse> customResponseList) {
this.customResponseList = customResponseList;
}
}
Main class:
- Call GsonRequest class with specified URL and followed by model class.
- Add Gson Request to Volley request queue.
public class CustomResponseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_response);
// If is the response is custom with object
customResponseObject();
}
private void customResponseObject() {
String url = "www.example.com"; // replace with your url.
GsonRequest gsonRequest = new GsonRequest(url, CustomResponseList.class, null, new Response.Listener() {
@Override
public void onResponse(Object response) {
// Handle response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
// Add gson request to volley request queue.
MainApplication.getInstance(this).addToRequestQueue(gsonRequest);
}
}
Reference code: https://github.com/rajeshreddy457/Volley-android-sample
Topics:
android,
gson,
library,
http,
java
Opinions expressed by DZone contributors are their own.
Comments