ArticleDetailFetcher.java
10.2 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.people.webview.model;
import android.text.TextUtils;
import com.people.webview.vm.IArticleDetailDataListener;
import com.wd.base.log.Logger;
import com.wd.capability.network.BaseObserver;
import com.wd.capability.network.RetrofitClient;
import com.wd.capability.network.constant.ParameterConstant;
import com.wd.capability.network.fetcher.BaseDataFetcher;
import com.wd.capability.network.interceptor.LoggingInterceptor;
import com.wd.capability.network.response.BaseResponse;
import com.wd.common.api.RequestApi;
import com.wd.foundation.bean.custom.content.ContentBean;
import com.wd.foundation.bean.custom.content.ContentTypeConstant;
import com.wd.foundation.bean.response.NewsDetailBean;
import com.wd.foundation.wdkit.system.DeviceUtil;
import com.wd.foundation.wdkit.utils.SpUtils;
import com.wd.foundation.wdkitcore.tools.JsonUtils;
import com.wd.foundation.wdkitcore.tools.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
/**
* 图文数据fetcher
*
* @author xujiawei
*/
public class ArticleDetailFetcher extends BaseDataFetcher<RequestApi> {
private IArticleDetailDataListener mDataListener;
public ArticleDetailFetcher(IArticleDetailDataListener dataListener) {
this.mDataListener = dataListener;
}
public void requestDetail(String id,String relId,String relType) {
Map<String, Object> params = new HashMap<>();
//稿件ID不能为空
if (StringUtils.isBlank(id)) {
if (mDataListener != null) {
mDataListener.onDetailDataError("");
}
return;
}
params.put(ParameterConstant.CONTENTID, id);
if (StringUtils.isNotBlank(relId)) {
params.put(ParameterConstant.CONTENT_REL_ID, relId);
}
if (StringUtils.isNotBlank(relType)) {
params.put(ParameterConstant.CONTENT_REL_TYPE, relType);
}
Observable<ResponseBody> phoneCode = getRetrofit().getArticleNewsDetail(params);
RetrofitClient.execute(phoneCode, new Observer<ResponseBody>() {
@Override
public void onSubscribe(@NotNull Disposable d) {
}
@Override
public void onNext(@NotNull ResponseBody responseBody) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(responseBody.string());
if (mDataListener != null) {
mDataListener.onDetailDataSuccess(jsonObject.toString());
}
} catch (Exception e) {
if (mDataListener != null) {
mDataListener.onDetailDataError(e.toString());
}
}
}
@Override
public void onError(@NotNull Throwable e) {
if (mDataListener != null) {
mDataListener.onDetailDataError(e.toString());
}
}
@Override
public void onComplete() {
}
});
}
public void requestPageData(String method, String url, JSONObject parameters,String callbackId) {
Observable<ResponseBody> phoneCode = null;
if (method.equals("POST") || method.equals("post")) {
phoneCode = getRetrofit().pageDataPost(url, getBody(parameters.toString()));
} else {
phoneCode = getRetrofit().pageDataGet(url, getMap(parameters));
}
RetrofitClient.execute(phoneCode, new Observer<ResponseBody>() {
@Override
public void onSubscribe(@NotNull Disposable d) {
}
@Override
public void onNext(@NotNull ResponseBody responseBody) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(responseBody.string());
if (mDataListener != null) {
mDataListener.onPageDataSuccess(url,jsonObject.toString(),callbackId);
}
} catch (Exception e) {
e.printStackTrace();
if (mDataListener != null) {
mDataListener.onPageDataError(e.toString(),callbackId);
}
}
}
@Override
public void onError(@NotNull Throwable e) {
if (mDataListener != null) {
mDataListener.onPageDataError(e.toString(),callbackId);
}
}
@Override
public void onComplete() {
}
});
}
@Override
public RequestBody getBody(Object object) {
String requestJson = JsonUtils.convertObjectToJson(object);
Logger.t(LoggingInterceptor.LOG_TAG).d("---请求data---" + requestJson);
MediaType mediaType = MediaType.Companion.parse(ParameterConstant.HEADER_JSON_TYPE);
RequestBody body = RequestBody.Companion.create(requestJson, mediaType);
return body;
}
public RequestBody getBody(String requestJson) {
Logger.t(LoggingInterceptor.LOG_TAG).d("---请求data---" + requestJson);
MediaType mediaType = MediaType.Companion.parse(ParameterConstant.HEADER_JSON_TYPE);
RequestBody body = RequestBody.Companion.create(requestJson, mediaType);
return body;
}
public Map<String, Object> getMap(JSONObject object) {
try {
Map<String, Object> objectMap = new HashMap<>();
for (Iterator<String> it = object.keys(); it.hasNext(); ) {
String key = it.next();
Object value = object.get(key);
if (value == null || value.toString().equals("null")) {
// objectMap.put(key,"");
} else {
if (value instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) value;
List<Object> objectList = new ArrayList<>();
for (int i = 0; i < jsonArray.length();i++) {
Object json = jsonArray.opt(i);
objectList.add(json);
}
objectMap.put(key, objectList);
} else {
objectMap.put(key, value);
}
}
}
return objectMap;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* 待处理关联关系
* @param id
* @param relType
* @param relId
*/
public void getNewsDetail(String id,String relType,String relId){
Map<String, Object> params = new HashMap<>();
//稿件ID不能为空
if (StringUtils.isBlank(id)) {
if (mDataListener != null) {
mDataListener.onGetNewsDetailFailed("");
}
return;
}
params.put(ParameterConstant.CONTENTID, id);
if (StringUtils.isNotBlank(relId)) {
params.put(ParameterConstant.CONTENT_REL_ID, relId);
}
if (StringUtils.isNotBlank(relType)) {
params.put(ParameterConstant.CONTENT_REL_TYPE, relType);
}
Observable<BaseResponse<List<NewsDetailBean>>> observable = getRetrofit().getOneNewsDetail(params);
request(observable, new BaseObserver<List<NewsDetailBean>>() {
@Override
protected void dealSpecialCode(int code, String message) {
if (mDataListener != null) {
mDataListener.onGetNewsDetailFailed(message);
}
}
@Override
protected void onSuccess(List<NewsDetailBean> newsDetailBeanList) {
if (mDataListener != null) {
mDataListener.onGetNewsDetailSuccess(newsDetailBeanList);
}
}
@Override
public void _onError(String e) {
if (mDataListener != null) {
mDataListener.onGetNewsDetailFailed(e);
}
}
});
}
/**
* 获取推荐列表
* @param contentId 当前数据id
* @param relId 当前内容关系id
* @param channelId 频道id
* @param recType 推荐类型
*/
public void getRecList(String contentId,String relId,String channelId,int recType){
Map<String, Object> params = new HashMap<>();
params.put(ParameterConstant.IMEI, DeviceUtil.getDeviceId());
String userId = SpUtils.getUserId();
if (!TextUtils.isEmpty(userId)) {
params.put(ParameterConstant.USER_ID, userId);
}
params.put(ParameterConstant.CONTENTID, contentId);
params.put("relId", relId);
//当前数据类型 1:视频,2:直播,5:专题,8:图文,9:组图,10:H5新闻
params.put(ParameterConstant.CONTENT_TYPE, ContentTypeConstant.URL_TYPE_EIGHT);
//推荐类型:1.详情推荐;2.搜索推荐
params.put("recType", recType);
//频道id
params.put("channelId", channelId);
Observable<BaseResponse<List<ContentBean>>> observable = getRetrofit().getRecList(getBody(params));
request(observable, new BaseObserver<List<ContentBean>>() {
@Override
protected void dealSpecialCode(int code, String message) {
if (mDataListener != null) {
mDataListener.onGetRecListFailed(message);
}
}
@Override
protected void onSuccess(List<ContentBean> operDataList) {
if (mDataListener != null) {
mDataListener.onGetRecListSuccess(operDataList);
}
}
@Override
public void _onError(String e) {
if (mDataListener != null) {
mDataListener.onGetRecListFailed("");
}
}
});
}
}