|
|
@@ -0,0 +1,183 @@
|
|
|
+package com.miekir.network.core;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.miekir.common.provider.CommonInstaller;
|
|
|
+import com.miekir.common.utils.ContextManager;
|
|
|
+import com.miekir.network.BuildConfig;
|
|
|
+import com.readystatesoftware.chuck.ChuckInterceptor;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.KeyStore;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.NoSuchProviderException;
|
|
|
+import java.security.cert.Certificate;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
+import java.security.cert.CertificateFactory;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import javax.net.ssl.SSLSocketFactory;
|
|
|
+import javax.net.ssl.TrustManagerFactory;
|
|
|
+
|
|
|
+import okhttp3.Interceptor;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.Response;
|
|
|
+import okhttp3.logging.HttpLoggingInterceptor;
|
|
|
+import retrofit2.Retrofit;
|
|
|
+import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
|
|
+import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @author Miekir
|
|
|
+ * @date 2020/1/19 10:58
|
|
|
+ * Description: 请求类,适用于请求链接和配置随时变动
|
|
|
+ */
|
|
|
+public class RetrofitHelper {
|
|
|
+ private static volatile RetrofitHelper mInstance;
|
|
|
+
|
|
|
+ private Context mContext;
|
|
|
+ private Retrofit mRetrofit;
|
|
|
+ private Retrofit mOtherRetrofit;
|
|
|
+
|
|
|
+ private Interceptor mHeaderInterceptor = new Interceptor() {
|
|
|
+ @Override
|
|
|
+ public Response intercept(Chain chain) throws IOException {
|
|
|
+ String token = "";
|
|
|
+ String email = "";
|
|
|
+// if (EdenManager.getInstance().isLogin()) {
|
|
|
+// token = EdenManager.getInstance().getBeiUser().token;
|
|
|
+// email = EdenManager.getInstance().getBeiUser().email;
|
|
|
+// }
|
|
|
+
|
|
|
+ Request.Builder builder = chain.request().newBuilder();
|
|
|
+ if (!TextUtils.isEmpty(token)) {
|
|
|
+ builder.addHeader("token", token)
|
|
|
+ .addHeader("email", email);
|
|
|
+ }
|
|
|
+
|
|
|
+ return chain.proceed(builder.build());
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ private static SSLSocketFactory getSSLContext (Context context){
|
|
|
+ try {
|
|
|
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
|
|
+ //privatekey.crt证书文件,将它放在Assets目录下
|
|
|
+ InputStream is = context.getAssets().open("lomsxj.crt");
|
|
|
+
|
|
|
+ Certificate ca = cf.generateCertificate(is);
|
|
|
+ Log.i("getSSLContext ", "ca=" + ((X509Certificate) ca).getSubjectDN());
|
|
|
+
|
|
|
+ KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
|
+ keyStore.load(null,null);
|
|
|
+ keyStore.setCertificateEntry("ca",ca);
|
|
|
+
|
|
|
+ TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
|
|
+ tmf.init(keyStore);
|
|
|
+
|
|
|
+ SSLContext ssContext = SSLContext.getInstance("TLSv1", "AndroidOpenSSL");
|
|
|
+ ssContext.init(null,tmf.getTrustManagers(),null);
|
|
|
+
|
|
|
+ return ssContext.getSocketFactory();
|
|
|
+
|
|
|
+ } catch (CertificateException | KeyManagementException | NoSuchProviderException | NoSuchAlgorithmException | KeyStoreException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private RetrofitHelper() {
|
|
|
+ mContext = ContextManager.getInstance().getContext();
|
|
|
+
|
|
|
+ // 默认请求的超时等配置
|
|
|
+ OkHttpClient httpClient = getDefaultOkHttpClient();
|
|
|
+
|
|
|
+ // 默认请求的地址、转换规则等配置,Base URL以不能包含路径,只能是http://加上IP或域名
|
|
|
+ mRetrofit = new Retrofit.Builder()
|
|
|
+ .baseUrl(BuildConfig.BASE_URL)
|
|
|
+ .client(httpClient)
|
|
|
+ .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
|
|
+ .addConverterFactory(GsonConverterFactory.create())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static RetrofitHelper getInstance() {
|
|
|
+ if (mInstance == null) {
|
|
|
+ init();
|
|
|
+ }
|
|
|
+ return mInstance;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static synchronized void init() {
|
|
|
+ if (mInstance == null) {
|
|
|
+ mInstance = new RetrofitHelper();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public OkHttpClient getDefaultOkHttpClient(boolean needLogin) {
|
|
|
+ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
|
|
|
+ @Override
|
|
|
+ public void log(String message) {
|
|
|
+ if (BuildConfig.IS_DEBUG_MODE) {
|
|
|
+ Log.i("Retrofit:", message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
|
|
+
|
|
|
+ OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
|
|
+ if (needLogin) {
|
|
|
+ builder.addInterceptor(mHeaderInterceptor);
|
|
|
+ }
|
|
|
+ return builder
|
|
|
+ .addInterceptor(interceptor)
|
|
|
+ //.sslSocketFactory(getSSLContext(context))
|
|
|
+ .addInterceptor(new ChuckInterceptor(mContext))
|
|
|
+ .retryOnConnectionFailure(true)
|
|
|
+ .connectTimeout(30, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(30, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(30, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @return 默认的请求设置
|
|
|
+ */
|
|
|
+ public OkHttpClient getDefaultOkHttpClient() {
|
|
|
+ return getDefaultOkHttpClient(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @return 请求接口
|
|
|
+ */
|
|
|
+ public <T> T getRequestApi(Class<T> apiClass) {
|
|
|
+ return mRetrofit.create(apiClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @return 请求接口
|
|
|
+ */
|
|
|
+ public <T> T getRequestApi(Class<T> apiClass, String baseUrl) {
|
|
|
+ OkHttpClient httpClient = getDefaultOkHttpClient(false);
|
|
|
+ mOtherRetrofit = new Retrofit.Builder()
|
|
|
+ .baseUrl(baseUrl)
|
|
|
+ .client(httpClient)
|
|
|
+ .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
|
|
+ .addConverterFactory(GsonConverterFactory.create())
|
|
|
+ .build();
|
|
|
+ return mOtherRetrofit.create(apiClass);
|
|
|
+ }
|
|
|
+}
|