RequestHandler.java
7.04 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.studymachine.www.http.model;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import androidx.lifecycle.LifecycleOwner;
import com.google.gson.JsonSyntaxException;
import com.studymachine.www.R;
import com.studymachine.www.manager.ActivityManager;
import com.studymachine.www.manager.UserManager;
import com.studymachine.www.ui.activity.LoginActivity;
import com.hjq.gson.factory.GsonFactory;
import com.hjq.http.EasyLog;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.config.IRequestHandler;
import com.hjq.http.exception.CancelException;
import com.hjq.http.exception.DataException;
import com.hjq.http.exception.HttpException;
import com.hjq.http.exception.NetworkException;
import com.hjq.http.exception.ResponseException;
import com.hjq.http.exception.ResultException;
import com.hjq.http.exception.ServerException;
import com.hjq.http.exception.TimeoutException;
import com.hjq.http.exception.TokenException;
import com.tencent.mmkv.MMKV;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import okhttp3.Headers;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* desc : 请求处理类
*/
public final class RequestHandler implements IRequestHandler {
private final Application mApplication;
private final MMKV mMmkv;
public RequestHandler(Application application) {
mApplication = application;
mMmkv = MMKV.mmkvWithID("http_cache_id");
}
@Override
public Object requestSucceed(LifecycleOwner lifecycle, IRequestApi api, Response response, Type type) throws Exception {
if (Response.class.equals(type)) {
return response;
}
if (!response.isSuccessful()) {
// 返回响应异常
throw new ResponseException(mApplication.getString(R.string.http_response_error) + ",responseCode:" + response.code() + ",message:" + response.message(), response);
}
if (Headers.class.equals(type)) {
return response.headers();
}
ResponseBody body = response.body();
if (body == null) {
return null;
}
if (InputStream.class.equals(type)) {
return body.byteStream();
}
String text;
try {
text = body.string();
} catch (IOException e) {
// 返回结果读取异常
throw new DataException(mApplication.getString(R.string.http_data_explain_error), e);
}
EasyLog.print(api.getApi());
// 打印这个 Json 或者文本
EasyLog.json(text);
EasyLog.print();
if (String.class.equals(type)) {
return text;
}
if (JSONObject.class.equals(type)) {
try {
// 如果这是一个 JSONObject 对象
return new JSONObject(text);
} catch (JSONException e) {
throw new DataException(mApplication.getString(R.string.http_data_explain_error), e);
}
}
if (JSONArray.class.equals(type)) {
try {
// 如果这是一个 JSONArray 对象
return new JSONArray(text);
} catch (JSONException e) {
throw new DataException(mApplication.getString(R.string.http_data_explain_error), e);
}
}
final Object result;
try {
result = GsonFactory.getSingletonGson().fromJson(text, type);
} catch (JsonSyntaxException e) {
// 返回结果读取异常
throw new DataException(mApplication.getString(R.string.http_data_explain_error), e);
}
if (result instanceof HttpData) {
HttpData<?> model = (HttpData<?>) result;
if (model.isRequestSucceed()) {
// 代表执行成功
return result;
}
if (model.isTokenFailure()) {
// 代表登录失效,需要重新登录
throw new TokenException(mApplication.getString(R.string.http_token_error));
}
// 代表执行失败
throw new ResultException(model.getMessage(), model);
}
return result;
}
@Override
public Exception requestFail(LifecycleOwner lifecycle, IRequestApi api, Exception e) {
// 判断这个异常是不是自己抛的
if (e instanceof HttpException) {
if (e instanceof TokenException) {
// 登录信息失效,跳转到登录页
UserManager.getInstance().logOut();
}
return e;
}
if (e instanceof SocketTimeoutException) {
return new TimeoutException(mApplication.getString(R.string.http_server_out_time), e);
}
if (e instanceof UnknownHostException) {
NetworkInfo info = ((ConnectivityManager) mApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
// 判断网络是否连接
if (info == null || !info.isConnected()) {
// 没有连接就是网络异常
return new NetworkException(mApplication.getString(R.string.http_network_error), e);
}
// 有连接就是服务器的问题
return new ServerException(mApplication.getString(R.string.http_server_error), e);
}
if (e instanceof IOException) {
//e = new CancelException(context.getString(R.string.http_request_cancel), e);
return new CancelException("", e);
}
return new HttpException(e.getMessage(), e);
}
@Override
public Object readCache(LifecycleOwner lifecycle, IRequestApi api, Type type) {
String cacheKey = GsonFactory.getSingletonGson().toJson(api);
String cacheValue = mMmkv.getString(cacheKey, null);
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
return null;
}
EasyLog.print("---------- cacheKey ----------");
EasyLog.json(cacheKey);
EasyLog.print("---------- cacheValue ----------");
EasyLog.json(cacheValue);
return GsonFactory.getSingletonGson().fromJson(cacheValue, type);
}
@Override
public boolean writeCache(LifecycleOwner lifecycle, IRequestApi api, Response response, Object result) {
String cacheKey = GsonFactory.getSingletonGson().toJson(api);
String cacheValue = GsonFactory.getSingletonGson().toJson(result);
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
return false;
}
EasyLog.print("---------- cacheKey ----------");
EasyLog.json(cacheKey);
EasyLog.print("---------- cacheValue ----------");
EasyLog.json(cacheValue);
return mMmkv.putString(cacheKey, cacheValue).commit();
}
}