| | |
| | | import android.util.Log; |
| | | |
| | | import com.driver.igotrip.BuildConfig; |
| | | import com.driver.igotrip.R; |
| | | import com.driver.igotrip.base.Const; |
| | | import com.driver.igotrip.base.MyApplication; |
| | | import com.driver.igotrip.utils.LogUtils; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.lang.reflect.Type; |
| | | import java.security.KeyStore; |
| | | import java.security.cert.CertificateFactory; |
| | | import java.security.cert.X509Certificate; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import javax.net.ssl.SSLSocketFactory; |
| | | import javax.net.ssl.TrustManager; |
| | | import javax.net.ssl.TrustManagerFactory; |
| | | import javax.net.ssl.X509TrustManager; |
| | | |
| | | import cn.sinata.xldutils.utils.SPUtils; |
| | | import io.reactivex.Observable; |
| | |
| | | */ |
| | | |
| | | public class HttpManager { |
| | | private static HttpManager httpManager; |
| | | private final ApiServics service = initBaseData().create(ApiServics.class); |
| | | private Gson gson; |
| | | private Map<String, String> headerParams; |
| | | private HashMap<String, Object> map; |
| | | private final Map<String, String> headerParams = new HashMap<>(); |
| | | |
| | | private static final class HttpManagerHolder { |
| | | private static final HttpManager httpManager = new HttpManager(); |
| | | } |
| | | |
| | | public static synchronized HttpManager getInstance() { |
| | | if (httpManager == null) synchronized (HttpManager.class) { |
| | | if (httpManager == null) { |
| | | httpManager = new HttpManager(); |
| | | } |
| | | } |
| | | return httpManager; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化请求头,具体情况根据需求设置 |
| | | */ |
| | | public void initHeader() { |
| | | headerParams = new HashMap<>(); |
| | | map = new HashMap<>(); |
| | | // 传参方式为json |
| | | // headerParams.put("Content-Type", "application/json"); |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | } |
| | | |
| | | public HashMap<String, Object> getMap() { |
| | | if (map == null) { |
| | | map = new HashMap<>(); |
| | | } |
| | | map.clear(); |
| | | return map; |
| | | return HttpManagerHolder.httpManager; |
| | | } |
| | | |
| | | private static String bodyToString(final RequestBody request) { |
| | |
| | | } |
| | | } |
| | | |
| | | private SSLSocketFactory getSSLSocketFactory(X509TrustManager trustManager) { |
| | | try { |
| | | SSLContext tls = SSLContext.getInstance("TLS"); |
| | | tls.init(null, new TrustManager[]{trustManager}, null); |
| | | return tls.getSocketFactory(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private X509TrustManager createTrustManager(int certificateResId) { |
| | | try { |
| | | CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
| | | try ( |
| | | // 读取证书文件 |
| | | InputStream inputStream = MyApplication.Companion.getAppContext().getResources().openRawResource(certificateResId) |
| | | ) { |
| | | X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream); |
| | | |
| | | // 创建包含该证书的KeyStore |
| | | KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| | | keyStore.load(null, null); |
| | | keyStore.setCertificateEntry("server", certificate); |
| | | |
| | | // 创建TrustManager |
| | | TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| | | trustManagerFactory.init(keyStore); |
| | | return (X509TrustManager) trustManagerFactory.getTrustManagers()[0]; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 初始化数据 |
| | | * |
| | | */ |
| | | private Retrofit initBaseData() { |
| | | initHeader(); |
| | | // 监听请求条件 |
| | | OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
| | | builder.connectTimeout(3, TimeUnit.MINUTES); |
| | | builder.readTimeout(3, TimeUnit.MINUTES); |
| | | builder.writeTimeout(3, TimeUnit.MINUTES); |
| | | X509TrustManager trustManager = createTrustManager(R.raw.star_gopost_ci); |
| | | if (trustManager != null) { |
| | | SSLSocketFactory sslSocketFactory = getSSLSocketFactory(trustManager); |
| | | if (sslSocketFactory != null) { |
| | | builder.sslSocketFactory(sslSocketFactory, trustManager); |
| | | } |
| | | } |
| | | // builder.addInterceptor(new Interceptor() { |
| | | // @Override |
| | | // public Response intercept(Chain chain) throws IOException { |
| | |
| | | } |
| | | |
| | | Log.i("zzz", "request====" + new JSONObject(params)); |
| | | |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | // getService.getResult(action.substring(action.lastIndexOf("/") + 1), headerParams, params) |
| | | service.getResult(action, headerParams, params) |
| | | .subscribeOn(Schedulers.io()) |
| | |
| | | int type = getTypeByLanguage(); |
| | | params.put(Const.language, type); // 1 中文 2 英文 3 法文 |
| | | LogUtils.d("post", params.toString()); |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | return service.postResult(action, headerParams, params); |
| | | } |
| | | |