package ruins.http;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * HttpClient
 */
@SuppressWarnings("unused")
public class HttpClient {
    private static HttpClient httpClient;
    private Retrofit retrofit;

    /**
     * 获取HttpClient
     * @param baseUrl  URL
     * @param log      是否打印日志
     */
    public static HttpClient getInstance(String baseUrl,boolean log){
        if (httpClient==null){
            synchronized (HttpClient.class){
                if (httpClient==null){
                    httpClient=new HttpClient(baseUrl,log);
                }
            }
        }
        return httpClient;
    }

    private HttpClient(String baseUrl,boolean log){
        OkHttpClient.Builder builder=new OkHttpClient.Builder();
        if (log){
            //Log信息拦截器
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            //设置Log
            builder.addInterceptor(interceptor);
        }

        OkHttpClient client = builder.build();
        retrofit=new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    public <T> T createService(Class<T> tClass){
        return retrofit.create(tClass);
    }
}

@@ -14,16 +14,55 @@
 .http;%0A%0A
+import java.util.concurrent.TimeUnit;%0A%0A
 import o
@@ -1214,24 +1214,88 @@
 ;%0A        %7D%0A
+        builder.connectTimeout(10, TimeUnit.SECONDS);   //%E8%AE%BE%E7%BD%AE%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B4
 %0A        OkH
@@ -1326,24 +1326,25 @@
 er.build();%0A
+%0A
         retr

设置超时时间。