Transmitting Network Data Using Volley - Sending a Simple Request
来源:互联网 发布:linux通配符的使用 编辑:程序博客网 时间:2024/05/22 10:28
At a high level, you use Volley by creating a RequestQueue
and passing it Request
objects. The RequestQueue
manages worker threads for running the network operations, reading from and writing to the cache, and parsing responses. Requests do the parsing of raw responses and Volley takes care of dispatching the parsed response back to the main thread for delivery.
This lesson describes how to send a request using theVolley.newRequestQueue
convenience method, which sets up a RequestQueue
for you. See the next lesson, Setting Up a RequestQueue, for information on how to set up aRequestQueue
yourself.
This lesson also describes how to add a request to aRequestQueue
and cancel a request.
Add the INTERNET Permission
To use Volley, you must add the android.permission.INTERNET
permission to your app's manifest. Without this, your app won't be able to connect to the network.
Use newRequestQueue
Volley provides a convenience method Volley.newRequestQueue
that sets up a RequestQueue
for you, using default values, 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 the main thread. Running on the main thread is convenient for populating UI controls with received data, as you can freely modify UI controls directly from your response handler, but it's especially critical to many of the important semantics provided by the library, particularly related to canceling requests.
See Setting Up a RequestQueue for a description of how to set up a RequestQueue
yourself, instead of using theVolley.newRequestQueue
convenience method.
Send a Request
To send a request, you simply construct one and add it to the RequestQueue
with add()
, as shown above. Once you add the request it moves through the pipeline, gets serviced, and has its raw response parsed and delivered.
When you call add()
, Volley runs one cache processing thread and a pool of network dispatch threads. When you add a request to the queue, it is picked up by the cache thread and triaged: if the request can be serviced from cache, the cached response is parsed on the cache thread and the parsed response is delivered on the main thread. If the request cannot be serviced from cache, it is placed on the network queue. The first available network thread takes the request from the queue, performs the HTTP transaction, parsse the response on the worker thread, writes the response to cache, and posts the parsed response back to the main thread for delivery.
Note that expensive operations like blocking I/O and parsing/decoding are done on worker threads. You can add a request from any thread, but responses are always delivered on the main thread.
Figure 1 illustrates the life of a request:
Cancel a Request
To cancel a request, call cancel()
on your Request
object. Once cancelled, Volley guarantees that your response handler will never be called. What this means in practice is that you can cancel all of your pending requests in your activity's onStop()
method and you don't have to litter your response handlers with checks forgetActivity() == null
, whether onSaveInstanceState()
has been called already, or other defensive boilerplate.
To take advantage of this behavior, you would typically have to track all in-flight requests in order to be able to cancel them at the appropriate time. There is an easier way: you can associate a tag object with each request. You can then use this tag to provide a scope of requests to cancel. For example, you can tag all of your requests with the Activity
they are being made on behalf of, and call requestQueue.cancelAll(this)
fromonStop()
. Similarly, you could tag all thumbnail image requests in a ViewPager
tab with their respective tabs and cancel on swipe to make sure that the new tab isn't being held up by requests from another one.
Here is an example that uses a string value for the tag:
- Define your tag and add it to your requests.
public 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);
- In your activity's
onStop()
method, cancel all requests that have this tag.@Overrideprotected void onStop () { super.onStop(); if (mRequestQueue != null) { mRequestQueue.cancelAll(TAG); }}
Take care when canceling requests. If you are depending on your response handler to advance a state or kick off another process, you need to account for this. Again, the response handler will not be called.
- Transmitting Network Data Using Volley - Sending a Simple Request
- Transmitting Network Data Using Volley 之Sending a Simple Request
- Transmitting Network Data Using Volley -Making a Standard Request
- Transmitting Network Data Using Volley -Implementing a Custom Request
- Transmitting Network Data Using Volley - Setting Up a RequestQueue
- 跟Google 学代码:Transmitting Network Data Using Volley
- Transmitting Network Data Using Volley(使用Volley框架传输网络数据)
- Transmitting Network Data Using Volley - Volley is an HTTP library that makes networking for Android
- Android训练课程(Android Training) - 使用Volley传输网络数据(Transmitting Network Data Using Volley)
- Volley发送简单的网络请求(Sending a Simple Request)
- Sending Simple Data to Other Apps
- 《Training:Sending Simple Data to Other Apps》
- A simple network …
- multipart data using Android Volley
- XMLHttpRequest - Sending a Request to the Server
- Sending Data Directly to a Printer
- How To Capture Data Packets On A Network Using Wireshark (a. k. a. Ethereal)
- [神经网络]1.4-Using neural nets to recognize handwritten digits-A simple network to classify ...(翻译)
- Android ADB 端口占用问题解决方案
- Scala类型 10 :交集类型(intersection type)与联合类型(union type)
- ubuntu12.04 安装配置jdk1.7
- MFC学习之路之UI --(4)防金山毒霸监控防御的列表框
- MyEclipse逆向生成实体类和Hibernate映射文件
- Transmitting Network Data Using Volley - Sending a Simple Request
- mongodb删除集合后磁盘空间不释放的问题
- (hdu step 5.1.3)Segment set(求与一条线段相交的线段集合中的线段的数量)
- MFC学习之路之UI --(5)美丽的很出名的换肤程序SlickSkins
- fiddler Android下https抓包全攻略
- 向微信公众号发送么个特地消息给么个人
- 批处理修改电源管理方案
- C/C++中static关键字及变量存储位置总结
- [Vim]恢复文件关闭之前光标的位置