| | |
| | | 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.Iterator; |
| | | 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; |
| | | import io.reactivex.Observer; |
| | | import io.reactivex.android.schedulers.AndroidSchedulers; |
| | | import io.reactivex.schedulers.Schedulers; |
| | | import okhttp3.Interceptor; |
| | | import okhttp3.MediaType; |
| | | import okhttp3.MultipartBody; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | import okhttp3.RequestBody; |
| | | import okhttp3.Response; |
| | | import okhttp3.ResponseBody; |
| | | import okhttp3.logging.HttpLoggingInterceptor; |
| | | import okio.Buffer; |
| | | import retrofit2.Retrofit; |
| | | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; |
| | |
| | | */ |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 初始化数据 |
| | | * |
| | | * @param action 当前请求的尾址 |
| | | */ |
| | | private Retrofit initBaseData(final String action) { |
| | | initHeader(); |
| | | private Retrofit initBaseData() { |
| | | // 监听请求条件 |
| | | 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 { |
| | |
| | | // return chain.proceed(request); |
| | | // } |
| | | // }); |
| | | builder.addInterceptor(new Interceptor() { |
| | | @Override |
| | | public Response intercept(Chain chain) throws IOException { |
| | | Request request = chain.request(); |
| | | Response proceed = chain.proceed(request); |
| | | return proceed; |
| | | } |
| | | }); |
| | | |
| | | HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); |
| | | httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY); |
| | | builder.addInterceptor(httpLoggingInterceptor); |
| | | |
| | | LoggingInterceptor loggingInterceptor = new LoggingInterceptor(); |
| | | loggingInterceptor.setLevel(BuildConfig.DEBUG ? LoggingInterceptor.Level.BODY : LoggingInterceptor.Level.NONE); |
| | | builder.addInterceptor(loggingInterceptor); |
| | | if (BuildConfig.DEBUG) { |
| | | builder.addInterceptor(new LoggingInterceptor()); |
| | | } |
| | | |
| | | Retrofit.Builder builder1 = new Retrofit.Builder() |
| | | .client(builder.build()) // 配置监听请求 |
| | |
| | | gson = new Gson(); |
| | | } |
| | | |
| | | ApiServics getService = initBaseData(action).create(ApiServics.class); |
| | | Log.i("zzz", "request====" + new JSONObject(params)); |
| | | |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | // getService.getResult(action.substring(action.lastIndexOf("/") + 1), headerParams, params) |
| | | getService.getResult(action, headerParams, params) |
| | | service.getResult(action, headerParams, params) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(observer); |
| | |
| | | // public <T extends BaseBean> void post(final String action, Map<String, Object> params, |
| | | public void post(final String action, Map<String, Object> params, |
| | | Observer<ResponseBody> observer) { |
| | | post(action, params) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(observer); |
| | | } |
| | | |
| | | /** |
| | | * Post请求 |
| | | * |
| | | * @param action 请求接口的尾址 |
| | | * @param params 请求参数 |
| | | */ |
| | | public Observable<ResponseBody> post(final String action, Map<String, Object> params) { |
| | | if (params == null) { |
| | | params = new HashMap<>(); |
| | | } |
| | | int type = getTypeByLanguage(); |
| | | params.put(Const.language, type); // 1 中文 2 英文 3 法文 |
| | | StringBuilder pwd = new StringBuilder(); |
| | | pwd.append(action); |
| | | if (params.size() != 0) { |
| | | pwd.append("?"); |
| | | } |
| | | Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator(); |
| | | while (iterator.hasNext()) { |
| | | Map.Entry<String, Object> entry = iterator.next(); |
| | | pwd.append(entry.getKey()).append("=").append(entry.getValue()); |
| | | if (iterator.hasNext()) { |
| | | pwd.append("&"); |
| | | } |
| | | } |
| | | |
| | | Map<String, Object> par = new HashMap<>(); |
| | | ApiServics jsonService = initBaseData(action).create(ApiServics.class); |
| | | LogUtils.d("post", params.toString()); |
| | | jsonService.postResult(action, headerParams, params) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(observer); |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | return service.postResult(action, headerParams, params); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param action 请求接口的尾址 |
| | | * @param fileName 文件名 |
| | | */ |
| | | public <T extends BaseBean> void post(final String action, File fileName, |
| | | Observer<ResponseBody> observer) { |
| | | |
| | | |
| | | ApiServics service = initBaseData(action).create(ApiServics.class); |
| | | |
| | | // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java |
| | | // use the FileUtils to get the actual file by uri |
| | | File file = fileName; |
| | | |
| | | // create RequestBody instance from file |
| | | RequestBody requestFile = |
| | | RequestBody.create(MediaType.parse("multipart/form-data"), file); |
| | | |
| | | // MultipartBody.Part is used to send also the actual file name |
| | | MultipartBody.Part body = |
| | | MultipartBody.Part.createFormData("file", file.getName(), requestFile); |
| | | service.upload(action, body) |
| | | public void post(final String action, File fileName, Observer<ResponseBody> observer) { |
| | | post(action, fileName) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(observer); |
| | | } |
| | | |
| | | /** |
| | | * Post请求 |
| | | * |
| | | * @param action 请求接口的尾址 |
| | | * @param file 文件 |
| | | */ |
| | | public Observable<ResponseBody> post(final String action, File file) { |
| | | |
| | | // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java |
| | | // use the FileUtils to get the actual file by uri |
| | | |
| | | // create RequestBody instance from file |
| | | RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); |
| | | |
| | | // MultipartBody.Part is used to send also the actual file name |
| | | MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile); |
| | | return service.upload(action, body); |
| | | } |
| | | |
| | | |