如何使用Volley网络请求队列?

时间:2014-02-28 21:49:30

标签: android android-volley

.add(新的网络调用我的Volley Request Queue,我已按照建议将其创建为单身人士。但我总是立即.start()这些网络电话。这始终是作为活动或片段中的动作完成的。

add方法甚至无法链接到start方法,例如.add(new volley request).start()

所以这假设我实际上正在管理(或想要管理)网络队列,而不是Volley处理队列的方式,我想。我应该将这些内容发送到IntentService并监听IntentService以将响应发送回我的Fragment / Activity吗?

2 个答案:

答案 0 :(得分:11)

如果您将requestQueue创建为:

requestQueue = Volley.newRequestQueue(mAppContext);

您不需要start()

根据Volley.RequestQueue的文档:“创建工作池的默认实例并在其上调用RequestQueue.start()。”

因此,您可以看到为什么您自己永远不需要致电start()

但是,如果您创建requestQueue(如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()必须被召唤。

PS:我获得了源代码本身提供的文档。 IDE可以毫不费力地提取它们。我只是将鼠标悬停在我需要的文件的方法/类名称上,然后按CTRL(在android-studio中)。

答案 1 :(得分:8)

作为一个排球用户,我可以告诉你,我从未调用.start()方法。我添加到队列中的所有请求都是自动启动的,我像你一样使用了单例类。