lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.kuanzhai.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);
//
//    }
 
}