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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
package com.example.toutiao.ui.page.newsChannel;
// ...
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static com.example.toutiao.ui.card.newsCardList.NewsCardItemDataModel.NO_IMAGE_TYPE;
import static com.example.toutiao.ui.card.newsCardList.NewsCardItemDataModel.ONE_IMAGE_TYPE;
import static com.example.toutiao.ui.card.newsCardList.NewsCardItemDataModel.THREE_IMAGE_TYPE;
/**
* A simple {@link Fragment} subclass.
* Use the {@link NewsChannelFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class NewsChannelFragment extends Fragment {
private static final String BASE_URL =
"https://www.toutiao.com/api/pc/feed/?max_behot_time=%d&category=%s";
private static final String[] CATEGORY_ATTR = new String[]{
"__all__",
"news_tech",
// "news_image",
"news_hot",
"news_entertainment",
"news_game",
"news_sports",
"news_finance",
"digital"
};
private static final String DEFAULT_AVATAR =
"https://img.88icon.com/download/jpg/20200901/84083236c883964781afea41f1ea4e9c_512_511.jpg!88bg";
private static final String DEFAULT_IMAGE =
"https://www.asiapacdigital.com/Zh_Cht/img/ap/services/reseller/TouTiao_1.jpg";
private static final int INIT_OR_REFRESH = 0;
private static final int LOAD_MORE = 1;
private static final int LOAD_FAIL = 2;
private ArrayList<NewsDataModel> mNewsDataModelList = new ArrayList<>();
private PageViewModel mPageViewModel;
private RecyclerView mCardListRecyclerView;
private NewsCardAdapter mCardListAdapter;
private RecyclerView.LayoutManager mCardListLayoutManager;
private Boolean mIsScrollToTop = false;
private final List<NewsCardItemDataModel> mCardDataModelList = new ArrayList<>();
private String mCategory;
private int mIndex;
private boolean mIsRefresh = false;
private boolean mIsLoadMore = false;
private boolean mIsLoadingFail = false;
private int mMaxBehotTime = 0;
private String mCookie;
public NewsChannelFragment() {
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*/
public static NewsChannelFragment newInstance(String category, int index) {
NewsChannelFragment fragment = new NewsChannelFragment();
Bundle bundle = new Bundle();
bundle.putString("category", category);
bundle.putInt("index", index);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
if (getArguments() != null) {
mCategory = getArguments().getString("category");
mIndex = getArguments().getInt("index");
}
mPageViewModel.setCategory(mCategory);
mPageViewModel.setIndex(mIndex);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news_channel, container, false);
// cardList
mCardListRecyclerView = view.findViewById(R.id.recycler_view_card_list);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mCardListRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mCardListLayoutManager = new LinearLayoutManager(getContext());
mCardListRecyclerView.setLayoutManager(mCardListLayoutManager);
// specify an adapter and pass in our data model list
mCardListAdapter = new NewsCardAdapter(mCardDataModelList, getContext());
mCardListRecyclerView.setAdapter(mCardListAdapter);
TextView mSectionLabelTextView = view.findViewById(R.id.text_view_section_label);
// init
getInitNews();
mPageViewModel.getCategory().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
mSectionLabelTextView.setText(s);
}
});
return view;
}
// render the recycler view card list when init and refreshing
public void initRenderCardList() {
Log.v("start init render", "render init card, news list size: " + mNewsDataModelList.size());
for (int i = 0; i < mNewsDataModelList.size(); i++) {
int type = mNewsDataModelList.get(i).getNewsCardStyleType();
String newsId = mNewsDataModelList.get(i).getNewsId();
String newsTitle = mNewsDataModelList.get(i).getNewsTitle();
String newsAbstract = mNewsDataModelList.get(i).getNewsAbstract();
int newsCommentsCount = mNewsDataModelList.get(i).getNewsCommentsCount();
String newsSource = mNewsDataModelList.get(i).getNewsSource();
String newsMediaAvatarUrl = mNewsDataModelList.get(i).getNewsMediaAvatarUrl();
String newsSourceUrl = mNewsDataModelList.get(i).getNewsSourceUrl();
if (type == NO_IMAGE_TYPE) {
mCardDataModelList.add(new NewsCardItemDataModel(
NO_IMAGE_TYPE,
newsId,
newsTitle,
newsAbstract,
newsCommentsCount,
newsSource,
newsMediaAvatarUrl,
newsSourceUrl
));
} else if (type == ONE_IMAGE_TYPE) {
String middleImage = mNewsDataModelList.get(i).getNewsImageUrl();
mCardDataModelList.add(new NewsCardItemDataModel(
ONE_IMAGE_TYPE,
newsId,
newsTitle,
newsAbstract,
newsCommentsCount,
newsSource,
newsMediaAvatarUrl,
newsSourceUrl,
middleImage
));
} else if (type == THREE_IMAGE_TYPE) {
ArrayList<String> newsThreeImage = mNewsDataModelList.get(i).getNewsThreeImage();
mCardDataModelList.add(new NewsCardItemDataModel(
THREE_IMAGE_TYPE,
newsId,
newsTitle,
newsAbstract,
newsCommentsCount,
newsSource,
newsMediaAvatarUrl,
newsSourceUrl,
newsThreeImage
));
}
}
mLoadingAnimationView.setVisibility(View.GONE);
mScreenMaskView.setVisibility(View.GONE);
mCardListRefreshLayout.finishRefreshing();
mCardListAdapter = new NewsCardAdapter(mCardDataModelList, getContext());
mCardListRecyclerView.setAdapter(mCardListAdapter);
mIsRefresh = false;
}
private String getUserAgent() {
String userAgent = "";
try {
userAgent = WebSettings.getDefaultUserAgent(getContext());
} catch (Exception e) {
userAgent = System.getProperty("https.agent");
}
StringBuffer sb = new StringBuffer();
for (int i = 0, length = userAgent.length(); i < length; i++) {
char c = userAgent.charAt(i);
if (c <= '\u001f' || c >= '\u007f') {
sb.append(String.format("\\u%04x", (int) c));
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* a methods to init card list
*
*/
public void getInitNews() {
// init data
mNewsDataModelList.clear();
mCardDataModelList.clear();
mLoadingAnimationView.setVisibility(View.VISIBLE);
mMaxBehotTime = 0;
// setting request
OkHttpClient client = new OkHttpClient();
Log.v("request url", String.format(Locale.ENGLISH, BASE_URL, mMaxBehotTime, CATEGORY_ATTR[mIndex]));
Request request = new Request.Builder()
.get()
.url(String.format(Locale.ENGLISH, BASE_URL, mMaxBehotTime, CATEGORY_ATTR[mIndex]))
.addHeader("User-Agent", getUserAgent())
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NotNull Call call, @NotNull final Response response) throws IOException {
Log.v("json status", " " + response.code());
Log.v("json body", " " + (response.body() != null));
String jsonData = response.body().string();
// deal with request body
dealWithResponseBody(jsonData);
mCookie = response.header("Set-Cookie");
Log.v("cookie", "set cookie: " + mCookie);
}
});
}
/**
* @param jsonData
*/
public void dealWithResponseBody(String jsonData) {
// avoid that jsonData is null
if(jsonData.length() < 1) {
mIsLoadingFail = true;
// run on main ui thread
mHander.sendEmptyMessage(LOAD_FAIL);
return;
}
Log.v("deal with response", "string to JsonObject");
Log.v("deal with response", "json data\n" + jsonData);
JsonObject result = JsonParser.parseString(jsonData).getAsJsonObject();
Log.v("deal with response", "result, before max_behot_time " + mMaxBehotTime);
Log.v("deal with response", String.valueOf(result.getAsJsonObject("next").has("max_behot_time")));
mMaxBehotTime = result.getAsJsonObject("next").get("max_behot_time").getAsInt();
Log.v("deal with response", "After max_behot_time : " + mMaxBehotTime + " get news object");
JsonArray newsData = result.getAsJsonArray("data");
for (int i = 0; i < newsData.size(); i++) {
Log.v("deal with response", "newsObjects " + i);
dealWithNewsObject(newsData.get(i).getAsJsonObject());
}
mHander.sendEmptyMessage(INIT_OR_REFRESH);
}
/**
* running on main UI thread to render card list
*/
private final Handler mHander = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
if (msg.what == INIT_OR_REFRESH) {
initRenderCardList();
} else if (msg.what == LOAD_MORE) {
loadMoreRenderCardList();
} else if (msg.what == LOAD_FAIL) {
loadingFail();
}
return false;
}
});
/**
* a methods to transfer JsonObject to NewsDataModel
*
* @param object
*/
public void dealWithNewsObject(JsonObject object) {
NewsDataModel temp;
// id
Log.v("deal with news object", "news_id " + object.get("group_id").getAsString());
String newsId = object.get("group_id").getAsString();
// title
Log.v("deal with news object", "news_title " + object.get("title").getAsString());
String newsTitle = object.get("title").getAsString();
// remove \r \n \t
newsTitle = newsTitle.replaceAll("\r|\n|\t", "");
// abstract
String newsAbstract = newsTitle;
Log.v("deal with news object", "news_abstract " + object.has("abstract"));
if (object.has("abstract")) {
newsAbstract = object.get("abstract").getAsString();
// remove \r \n \t
newsAbstract = newsAbstract.replaceAll("\r|\n|\t", "");
}
// comments count
Log.v("deal with news object", "news_comments_count " + object.has("comments_count"));
int newsCommentsCount = 0;
if (object.has("comments_count")) {
newsCommentsCount = object.get("comments_count").getAsInt();
}
// news source
Log.v("deal with news object", "news_source " + object.get("source").getAsString());
String newsSource = object.get("source").getAsString();
// news media avatar url
String newsMediaAvatarUrl = DEFAULT_AVATAR;
if (object.has("media_avatar_url")) {
newsMediaAvatarUrl = "https:" + object.get("media_avatar_url").getAsString();
}
// news source url
Log.v("deal with news object", "news_source_url " + object.get("source_url").getAsString());
String newsSourceUrl = object.get("source_url").getAsString();
Log.v("deal with news object", "have image_list " + object.has("image_list"));
Log.v("deal with news object", "single_mode " + object.get("single_mode").getAsBoolean());
// three image style
if (object.has("image_list")) {
JsonArray imageList = object.get("image_list").getAsJsonArray();
ArrayList<String> newsThreeImage = new ArrayList<>();
// the Json Array is not null
if(imageList.size() < 3) {
for (int i = 0; i < 3; i++) {
// avoid url is null
newsThreeImage.add(DEFAULT_IMAGE);
}
} else {
for (int i = 0; i < 3; i++) {
// avoid url is null
String url = imageList.get(i).getAsJsonObject().get("url").getAsString();
if(url.length() == 0) {
url = DEFAULT_IMAGE;
} else {
url = "https:" + url;
}
newsThreeImage.add(url);
}
}
temp = new NewsDataModel(
NewsDataModel.THREE_IMAGE_TYPE,
newsId,
newsTitle,
newsAbstract,
newsCommentsCount,
newsSource,
newsMediaAvatarUrl,
newsSourceUrl,
newsThreeImage
);
}
// one image style
else if (object.get("single_mode").getAsBoolean()) {
String middleImage = "https:" + object.get("image_url").getAsString();
temp = new NewsDataModel(
NewsDataModel.ONE_IMAGE_TYPE,
newsId,
newsTitle,
newsAbstract,
newsCommentsCount,
newsSource,
newsMediaAvatarUrl,
newsSourceUrl,
middleImage
);
}
// no image style
else {
temp = new NewsDataModel(
NewsDataModel.NO_IMAGE_TYPE,
newsId,
newsTitle,
newsAbstract,
newsCommentsCount,
newsSource,
newsMediaAvatarUrl,
newsSourceUrl
);
}
mNewsDataModelList.add(temp);
}
}
|