How do I use the Volley network request queue?

人盡茶涼 提交于 2019-12-30 04:44:07

问题


I .add( new network calls to my Volley Request Queue which I have created as a singleton as suggested. But I always immediately .start() these network calls. This is always done as an action in an activity or a fragment.

The add method cannot even be chained into a start method, like .add(new volley request).start()

So this assumes I am actually managing (or wanting to manage) a network queue somewhere, outside of the way Volley handles its queue, I guess. Should I be sending these things to an IntentService and listening for the IntentService to send a response back to my Fragment/Activity?


回答1:


As a volley user I can tell you that I have never called .start() method. all the requests i've added to the queue started automatically, I used singleton class like you did.




回答2:


If you create a requestQueue as:

requestQueue = Volley.newRequestQueue(mAppContext);

you will not need start().

According to docs of Volley.RequestQueue : "Creates a default instance of the worker pool and calls RequestQueue.start() on it."

Hence you can see why you never needed to call start() yourself.

However, if you create a requestQueue as (as shown in the official reference):

RequestQueue mRequestQueue;

// 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.
mRequestQueue = new RequestQueue(cache, network);

// Start the queue
mRequestQueue.start();

start() will have to be called.

PS: I get the documentation as provided in the source code itself. IDEs are able to extract them effortlessly. I just hover over the method/class name whose document I need and press CTRL (in android-studio).



来源:https://stackoverflow.com/questions/22106483/how-do-i-use-the-volley-network-request-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!