| | |
| | | package com.driver.igotrip.netUtls; |
| | | |
| | | |
| | | import static com.driver.igotrip.netUtls.NetKitKt.getToken; |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.driver.igotrip.BuildConfig; |
| | |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Type; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | 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; |
| | | import retrofit2.converter.gson.GsonConverterFactory; |
| | | |
| | | import static com.driver.igotrip.netUtls.NetKitKt.getToken; |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | 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; |
| | |
| | | map = new HashMap<>(); |
| | | // 传参方式为json |
| | | // headerParams.put("Content-Type", "application/json"); |
| | | headerParams.put("Authorization", "Bearer "+getToken()); |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | } |
| | | |
| | | public HashMap<String, Object> getMap() { |
| | |
| | | /** |
| | | * 初始化数据 |
| | | * |
| | | * @param action 当前请求的尾址 |
| | | */ |
| | | private Retrofit initBaseData(final String action) { |
| | | 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); |
| | | builder.readTimeout(3, TimeUnit.MINUTES); |
| | | builder.writeTimeout(3, TimeUnit.MINUTES); |
| | | // 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()) // 配置监听请求 |
| | |
| | | * @param observer observer |
| | | */ |
| | | // public <T extends BaseBean> void get(final String action, |
| | | public void get(final String action, Map<String, String> params, Observer<ResponseBody> observer) { |
| | | public void get(final String action, Map<String, String> params, Observer<ResponseBody> observer) { |
| | | if (params == null) { |
| | | params = new HashMap<>(); |
| | | } |
| | | int type = getTypeByLanguage(); |
| | | params.put(Const.language,type+""); // 1 中文 2 英文 3 法文 |
| | | params.put(Const.language, type + ""); // 1 中文 2 英文 3 法文 |
| | | if (gson == null) { |
| | | gson = new Gson(); |
| | | } |
| | | |
| | | ApiServics getService = initBaseData(action).create(ApiServics.class); |
| | | Log.i("zzz", "request====" + new JSONObject(params)); |
| | | |
| | | // 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 static int getTypeByLanguage() { |
| | | String lang = SPUtils.getString(Const.language, Const.Language.DEFAULT_LANGUAGE); |
| | | int type = 0; |
| | | if (lang == null || lang.isEmpty() || lang.equals(Const.Language.ENGLISH)){ |
| | | type = 2; |
| | | } |
| | | else if (lang.equals(Const.Language.CHINESE) || lang.equals("zh")){ |
| | | type = 1; |
| | | } |
| | | else if (lang.equals(Const.Language.FRENCH)){ |
| | | if (lang == null || lang.isEmpty() || lang.equals(Const.Language.FRENCH)) { |
| | | type = 3; |
| | | } else if (lang.equals(Const.Language.CHINESE) || lang.equals("zh")) { |
| | | type = 1; |
| | | } else if (lang.equals(Const.Language.ENGLISH)) { |
| | | type = 2; |
| | | } |
| | | return type; |
| | | } |
| | |
| | | */ |
| | | // 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) { |
| | | 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) |
| | | 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 法文 |
| | | LogUtils.d("post", params.toString()); |
| | | return service.postResult(action, headerParams, params); |
| | | } |
| | | |
| | | /** |
| | | * 将map数据转换为 普通的 json RequestBody |
| | | * |
| | | * @param map 以前的请求参数 |
| | | * @return |
| | | */ |
| | | public static RequestBody convertMapToBody(Map<?,?> map) { |
| | | public static RequestBody convertMapToBody(Map<?, ?> map) { |
| | | return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new JSONObject(map).toString()); |
| | | } |
| | | |
| | |
| | | * @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); |
| | | } |
| | | |
| | | |