| | |
| | | */ |
| | | |
| | | 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 Retrofit initBaseData() { |
| | | initHeader(); |
| | | // 监听请求条件 |
| | | OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
| | | builder.connectTimeout(3, TimeUnit.MINUTES); |
| | |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | |