如何在Retrofit中设置最大网络连接数

时间:2014-12-23 09:33:12

标签: retrofit android-networking androidhttpclient aquery

我正在浏览一些AQuery代码here,发现有一种方法可以修改AQuery中的网络连接数。

有没有办法在改造中这样做,改造的默认值是什么?

/* Settings of Image */
//set the max number of concurrent network connections, default is 4
AjaxCallback.setNetworkLimit(8);

//set the max number of icons (image width <= 50) to be cached in memory, default is 20
BitmapAjaxCallback.setIconCacheLimit(50);

//set the max number of images (image width > 50) to be cached in memory, default is 20
BitmapAjaxCallback.setCacheLimit(50);

aq = new AQuery(context);

1 个答案:

答案 0 :(得分:3)

例如在Retrofit中的默认连接数有点按需,即为每个新Runnable(连接)创建/重用新线程,该线程被送到Executor

您可以通过限制Thread的数量来限制网络连接。在构建RestAdapter时:

restAdapterBuilder.setExecutors(Executors.newCachedThreadPool(numberOfConnections), new MainThreadExecutor());

restAdapterBuilder.setExecutors(Executors.newFixedThreadPool(numberOfConnections), new MainThreadExecutor());

这与AQuery限制连接数的方式完全相同。

有关详情,请参阅Executors