OKHttp的單例模式
阿新 • • 發佈:2019-02-16
import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; /** * Created by d on 2015/12/5. */ public class OkHttpUtils { private static OkHttpClient client = null; public static OkHttpClient getInstance(){ if(client==null){ synchronized (OkHttpUtils.class){ if(client==null){ client = new OkHttpClient.Builder() .writeTimeout(20000, TimeUnit.SECONDS) .connectTimeout(20000,TimeUnit.SECONDS) .readTimeout(20000,TimeUnit.SECONDS) .build(); } } } returnclient; } }