Skip to content

Instantly share code, notes, and snippets.

@paulonteri
Last active June 23, 2020 14:06
Show Gist options
  • Save paulonteri/8f5f8e55328b178ee189f94d2a40e96f to your computer and use it in GitHub Desktop.
Save paulonteri/8f5f8e55328b178ee189f94d2a40e96f to your computer and use it in GitHub Desktop.
Android Volley Request Queue
RequestQueue requestQueue;
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
requestQueue = new RequestQueue(cache, network);
// Start the queue
requestQueue.start();
String url ="http://www.example.com";
// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment