package com.zhaoyang.driver.netUtls;
|
|
|
import com.google.gson.Gson;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.concurrent.TimeUnit;
|
|
import io.reactivex.Observer;
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
import io.reactivex.schedulers.Schedulers;
|
import okhttp3.OkHttpClient;
|
import okhttp3.ResponseBody;
|
import retrofit2.Retrofit;
|
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
/**
|
* 网络请求管理类
|
* Created by Administrator on 2018/1/25.
|
*/
|
|
public class FaceHttpManager {
|
private static FaceHttpManager httpManager;
|
private Gson gson;
|
private Map<String, String> headerParams;
|
private HashMap<String, Object> map;
|
|
public static synchronized FaceHttpManager getInstance() {
|
if (httpManager == null) synchronized (FaceHttpManager.class) {
|
if (httpManager == null) {
|
httpManager = new FaceHttpManager();
|
}
|
}
|
return httpManager;
|
}
|
|
|
/**
|
* 初始化请求头,具体情况根据需求设置
|
*/
|
public void initHeader() {
|
headerParams = new HashMap<>();
|
map = new HashMap<>();
|
// 传参方式为json
|
headerParams.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
headerParams.put("Authorization", "APPCODE " + "b7d32437d08149099457dcb50fb57df2");
|
}
|
|
public HashMap<String, Object> getMap() {
|
if (map == null) {
|
map = new HashMap<>();
|
}
|
map.clear();
|
return map;
|
}
|
|
|
|
/**
|
* 初始化数据
|
*
|
* @param action 当前请求的尾址
|
*/
|
private Retrofit initBaseData(final String action) {
|
initHeader();
|
// 监听请求条件
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
builder.connectTimeout(20, TimeUnit.SECONDS);
|
Retrofit.Builder builder1 = new Retrofit.Builder()
|
.client(builder.build()) // 配置监听请求
|
.addConverterFactory(GsonConverterFactory.create()) // 请求结果转换(当前为GSON)
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()); // 请求接受工具(当前为RxJava2)
|
builder1.baseUrl("https://sfzread.market.alicloudapi.com/");
|
|
return builder1.build();
|
}
|
|
|
/**
|
* Post请求
|
*
|
* @param action 请求接口的尾址
|
* @param params 请求参数
|
*/
|
public void post(final String action, Map<String, Object> params,
|
Observer<ResponseBody> observer) {
|
if (params == null) {
|
params = new HashMap<>();
|
}
|
ApiServics jsonService = initBaseData(action).create(ApiServics.class);
|
jsonService.postResult(action, headerParams,params)
|
.subscribeOn(Schedulers.io())
|
.observeOn(AndroidSchedulers.mainThread())
|
.subscribe(observer);
|
}
|
|
/**
|
* Get请求
|
*
|
* @param action 请求接口的尾址
|
* @param params 请求参数
|
* @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) {
|
if (params == null) {
|
params = new HashMap<>();
|
}
|
if (gson == null) {
|
gson = new Gson();
|
}
|
|
ApiServics getService = initBaseData(action).create(ApiServics.class);
|
getService.getResult(action, headerParams, params)
|
.subscribeOn(Schedulers.io())
|
.observeOn(AndroidSchedulers.mainThread())
|
.subscribe(observer);
|
}
|
|
//
|
// /**
|
// * Post请求
|
// *
|
// * @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);
|
// File file = fileName;
|
// RequestBody requestFile =
|
// RequestBody.create(MediaType.parse("multipart/form-data"), file);
|
// MultipartBody.Part body =
|
// MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
|
// service.upload(action, body)
|
// .subscribeOn(Schedulers.io())
|
// .observeOn(AndroidSchedulers.mainThread())
|
// .subscribe(observer);
|
//
|
// }
|
|
}
|