Retrofit+Okhttp

package wld.bawei.com.myzhoukaoone.MVP;

import android.util.Log;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.GsonConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit;
import wld.bawei.com.myzhoukaoone.Bean.Bean_Shop;
import wld.bawei.com.myzhoukaoone.PageService;

/**
 * Created by 大金鑫 on 2017/12/30.
 */

public class Shop_OkHttp {
    private static Shop_OkHttp shop_okHttp;
    private Shop_OkHttp(){}
    public static Shop_OkHttp getShop(){
        if(shop_okHttp==null){
            synchronized (Shop_OkHttp.class){
                if(shop_okHttp==null){
                    shop_okHttp=new Shop_OkHttp();
                }
            }
        }
        return shop_okHttp;
    }
    public void doPost(String mUrl, final Shop_CallBack shop_callBack){
        Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));//线程池
        dispatcher.setMaxRequests(20);//最大的请求数量
        dispatcher.setMaxRequestsPerHost(1);//主机同一个时间,最大的请求数量

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .dispatcher(dispatcher)
                .connectionPool(new ConnectionPool(100,30, TimeUnit.SECONDS))
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
        PageService pageService = retrofit.create(PageService.class);
        pageService.get().enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Bean_Shop body = response.body();
                 //接口回调
                shop_callBack.onSusses(response.body());
            }
            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });
    }
}

你可能感兴趣的:(Retrofit+Okhttp)