// Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override publicvoidonResponse(String response){ // Display the first 500 characters of the response string. mTextView.setText("Response is: "+ response.substring(0,500)); } }, new Response.ErrorListener() { @Override publicvoidonErrorResponse(VolleyError error){ mTextView.setText("That didn't work!"); } }); // Add the request to the RequestQueue. queue.add(stringRequest);
取消一个Request
可以使用cacel方法取消一个Request,防止其总是在连接网络.
所以常在onStop()方法中调用cancel相关方法.
1 2 3 4 5 6 7 8 9
ublic static final String TAG = "MyTag"; StringRequest stringRequest; // Assume this exists. RequestQueue mRequestQueue; // Assume this exists.
// Set the tag on the request. stringRequest.setTag(TAG);
// Add the request to the RequestQueue. mRequestQueue.add(stringRequest);