Volley provides a convenience method Volley.newRequestQueue
that sets up a RequestQueue
for you, using defaultvalues,
and starts the queue. For example:
final TextView mTextView = (TextView) findViewById(R.id.text); ... // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); String url ="http://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // Display the first 500 characters of the response string. mTextView.setText("Response is: "+ response.substring(0,500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mTextView.setText("That didn't work!"); } }); // Add the request to the RequestQueue. queue.add(stringRequest);
Volley always delivers parsed responses on themain thread. Running on the main thread is convenient for populating UIcontrols with received data, as you can freely modify UI controls directly fromyour response handler, but it's especially critical to many of the importantsemantics provided by the library, particularly related to canceling requests.
See SettingUp a RequestQueue for a description of how to set up aRequestQueue
yourself,instead
of using theVolley.newRequestQueue
conveniencemethod.