How to set the maximum number of network connections in Retrofit

﹥>﹥吖頭↗ 提交于 2019-12-11 02:55:15

问题


I was going through some AQuery code here and found there's a way to modify the number of network connections in AQuery.

Is there a way of doing this in retrofit, and what are the default values for retrofit?

/* 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:


Default number of connection for instance in Retrofit is somewhat on-demand, i.e new thread is created/reused for each new Runnable (connection) that is fed to the Executor

You can limit network connection by limiting number of Thread. When you build your RestAdapter do:

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

or

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

This is exactly the same what AQuery does to limit the number of connections.

See Executors for more



来源:https://stackoverflow.com/questions/27617583/how-to-set-the-maximum-number-of-network-connections-in-retrofit

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