通过RoboSpice异步执行一堆请求的最简单方法是什么?

我在某处读到需要实现RequestRunner的内容,但我不知道将其与SpiceManager合并的方法,有什么想法吗?

最佳答案

您可以覆盖可用线程数,定义自己的自定义SpiceService:

    public class CustomSpiceService extends RetrofitGsonSpiceService {
      /**
      * Overrides the number of threads that will be used to make requests.  The default
      * is 1.
      */
      @Override
      public int getThreadCount(){
        return NUM_THREAD;
      }
    }


之后,您可以在管理器中使用新的spiceService:

    private SpiceManager spiceManager = new SpiceManager(CustomSpiceService.class);


另外,您可以检测连接的类型,因此,如果您处于Wifi连接中,则可以有更多线程。

/**
 * Overrides the number of threads that will be used to make requests.  The default
 * is 1, so if we are on a fast connection we use 4, otherwise we use 2.
 */
@Override
public int getThreadCount() {

    ConnectivityManager connectivityManager =
            (ConnectivityManager) DaftApp.getInstance().getSystemService(CONNECTIVITY_SERVICE);

    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
   if(info==null){
       return 2; // there is no network available now. Anyway we use the default num of thread
   }
    switch (info.getType()) {
        case ConnectivityManager.TYPE_WIFI:
        case ConnectivityManager.TYPE_WIMAX:
        case ConnectivityManager.TYPE_ETHERNET:
            return 4;
        case ConnectivityManager.TYPE_MOBILE:
            return 2;
        default:
            return 2;
    }
}

关于android - RoboSpice并行执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22117740/

10-12 04:36