Execute http request in parallel with Retrofit 2

后端 未结 1 475
一整个雨季
一整个雨季 2021-01-08 01:26

I want to implement multiple parallel request in Retrofit 2. I have the following structure to make 3 request :

HistoricalRApi.IStockChart service=Historical         


        
相关标签:
1条回答
  • 2021-01-08 02:08

    Thanks to Colin Gillespie link i have implemented what Jake Wharton says and this is the result:

     public static IStockChart getMyApiService() {
            OkHttpClient client=new OkHttpClient();
            Dispatcher dispatcher=new Dispatcher();
            dispatcher.setMaxRequests(3);
            client.setDispatcher(dispatcher);
           // OkHttpClient client = new OkHttpClient();
           //  HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
           //  interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
           //  client.interceptors().add(interceptor);
            if(myService ==null){
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://chartapi.finance.yahoo.com/")
                        .addConverterFactory(JsonpGsonConverterFactory.create())
                        .client(client)
                        .build();
                myService=retrofit.create(IStockChart.class);
                return myService;
            } else {
                return myService;
            }
    
    
    
        }
    
    0 讨论(0)
提交回复
热议问题