-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApacheFragment.java
337 lines (305 loc) · 14.2 KB
/
ApacheFragment.java
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
package com.catherine.webservices.fragments;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import com.catherine.webservices.Constants;
import com.catherine.webservices.MyApplication;
import com.catherine.webservices.R;
import com.catherine.webservices.adapters.TextCardRVAdapter;
import com.catherine.webservices.components.DialogManager;
import com.catherine.webservices.entities.TextCard;
import com.catherine.webservices.interfaces.MainInterface;
import com.catherine.webservices.interfaces.OnItemClickListener;
import com.catherine.webservices.network.HttpRequest;
import com.catherine.webservices.network.HttpResponse;
import com.catherine.webservices.network.HttpResponseListener;
import com.catherine.webservices.network.MyApache;
import com.catherine.webservices.network.NetworkHealthListener;
import com.catherine.webservices.network.NetworkHelper;
import com.catherine.webservices.toolkits.CLog;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jetbrains.annotations.NotNull;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* Created by Catherine on 2017/8/25.
* Soft-World Inc.
* catherine919@soft-world.com.tw
*/
public class ApacheFragment extends LazyFragment {
public final static String TAG = ApacheFragment.class.getSimpleName();
private List<TextCard> entities;
private SwipeRefreshLayout srl_container;
private Handler networkTask;
private MyApache myApache;
private Callback callback;
private TextCardRVAdapter adapter;
private HandlerThread handlerThreadB, handlerThreadC;
private MainInterface mainInterface;
private NetworkHealthListener networkHealthListener;
private boolean retry;
private int step;
public static ApacheFragment newInstance(boolean isLazyLoad) {
Bundle args = new Bundle();
args.putBoolean(LazyFragment.INTENT_BOOLEAN_LAZYLOAD, isLazyLoad);
ApacheFragment fragment = new ApacheFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreateViewLazy(Bundle savedInstanceState) {
super.onCreateViewLazy(savedInstanceState);
setContentView(R.layout.f_apache);
fillInData();
initComponent();
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private void fillInData() {
entities = new ArrayList<>();
entities.add(new TextCard("GET " + Constants.HOST + " in looper A", "Connect to the server with user-defined headers", null));
entities.add(new TextCard("POST " + Constants.HOST + " in looper A", "Connect to the server with correct account", null));
entities.add(new TextCard("POST " + Constants.HOST + " in looper A", "Connect to the server with false Authorization", null));
entities.add(new TextCard("POST " + Constants.HOST + " in looper B", "Connect to the server with false account", null));
entities.add(new TextCard("GET http://dictionary.cambridge.org/ in looper C", "Connect to Cambridge dictionary server", null));
entities.add(new TextCard("GET " + Constants.GITHUB_API_DOMAIN + " in looper C", "Connect to GitHub api with gzip encoding", null));
}
private void initComponent() {
mainInterface = (MainInterface) getActivity();
networkHealthListener = new NetworkHealthListener() {
@Override
public void networkConnected(@NotNull String type) {
CLog.i(TAG, "network connected:" + type);
if (retry) {
retry = false;
connect(step);
}
}
@Override
public void networkDisable() {
CLog.e(TAG, "network disable");
}
};
mainInterface.listenToNetworkState(networkHealthListener);
myApache = new MyApache();
callback = new Callback();
handlerThreadB = new HandlerThread("Looper B");
handlerThreadB.start();
handlerThreadC = new HandlerThread("Looper C");
handlerThreadC.start();
networkTask = new Handler(MyApplication.INSTANCE.calHandlerThread.getLooper());
srl_container = (SwipeRefreshLayout) findViewById(R.id.srl_container);
srl_container.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent, R.color.colorPrimaryDark, R.color.colorAccentDark);
srl_container.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
fillInData();
initComponent();
srl_container.setRefreshing(false);
}
});
RecyclerView rv_main_list = (RecyclerView) findViewById(R.id.rv_main_list);
rv_main_list.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
adapter = new TextCardRVAdapter(getActivity(), entities, new OnItemClickListener() {
@Override
public void onItemClick(View view, final int position) {
connect(position);
}
@Override
public void onItemLongClick(@NotNull View view, int position) {
}
});
rv_main_list.setAdapter(adapter);
adapter.setFromHtml(true);
}
private void connect(final int position) {
step = position;
retry = false;
switch (position) {
case 0:
srl_container.setRefreshing(true);
networkTask.post(new Runnable() {
@Override
public void run() {
Map<String, String> headers = MyApache.getDefaultHeaders();
headers.put("h1", "Hi there!");
headers.put("h2", "I am a mobile phone.");
headers.put("Authorization", Constants.AUTHORIZATION);
callback.setPosition(position);
myApache.doGet(new HttpRequest.Builder()
.url(Constants.HOST + "LoginServlet?name=zhangsan&password=123456")
.headers(headers)
.listener(callback)
.build());
}
});
break;
case 1:
srl_container.setRefreshing(true);
networkTask.post(new Runnable() {
@Override
public void run() {
Map<String, String> headers = MyApache.getDefaultHeaders();
headers.put("Authorization", Constants.AUTHORIZATION);
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("name", "zhangsan"));
nameValuePairs.add(new BasicNameValuePair("password", "123456"));
callback.setPosition(position);
myApache.doPost(nameValuePairs, new HttpRequest.Builder()
.url(Constants.HOST + "LoginServlet")
.headers(headers)
.listener(callback)
.build());
}
});
break;
case 2:
srl_container.setRefreshing(true);
networkTask.post(new Runnable() {
@Override
public void run() {
Map<String, String> headers = MyApache.getDefaultHeaders();
headers.put("Authorization", "12345");
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("name", "zhangsan"));
nameValuePairs.add(new BasicNameValuePair("password", "123456"));
callback.setPosition(position);
myApache.doPost(nameValuePairs, new HttpRequest.Builder()
.url(Constants.HOST + "LoginServlet")
.headers(headers)
.listener(callback)
.build());
}
});
break;
case 3:
srl_container.setRefreshing(true);
Handler networkTaskB = new Handler(handlerThreadB.getLooper());
networkTaskB.post(new Runnable() {
@Override
public void run() {
Map<String, String> headers = MyApache.getDefaultHeaders();
headers.put("Authorization", Constants.AUTHORIZATION);
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("name", ""));
nameValuePairs.add(new BasicNameValuePair("password", ""));
callback.setPosition(position);
myApache.doPost(nameValuePairs, new HttpRequest.Builder()
.url(Constants.HOST + "LoginServlet")
.headers(headers)
.listener(callback)
.build());
}
});
break;
case 4:
srl_container.setRefreshing(true);
Handler networkTaskC = new Handler(handlerThreadC.getLooper());
networkTaskC.post(new Runnable() {
@Override
public void run() {
callback.setPosition(position);
myApache.doGet(new HttpRequest.Builder()
.url("http://dictionary.cambridge.org/dictionary/english-chinese-simplified/philosopher")
.listener(callback)
.build());
}
});
break;
case 5:
srl_container.setRefreshing(true);
Handler networkTaskC2 = new Handler(handlerThreadC.getLooper());
networkTaskC2.post(new Runnable() {
@Override
public void run() {
callback.setPosition(position);
myApache.doGet(new HttpRequest.Builder()
.url(Constants.GITHUB_API_DOMAIN + "users/Catherine22/repos")
.listener(callback)
.build());
}
});
break;
}
}
private class Callback implements HttpResponseListener {
private int position;
void setPosition(int position) {
this.position = position;
}
@Override
public void connectSuccess(HttpResponse response) {
//Running in a non-UI thread right now.
CLog.i(TAG, String.format(Locale.ENGLISH, "connectSuccess code:%s, message:%s, body:%s", response.getCode(), response.getCodeString(), response.getBody()));
TextCard tc = entities.get(position);
tc.contents = String.format(Locale.ENGLISH, "connectSuccess code:%s, message:%s, body:%s", response.getCode(), response.getCodeString(), response.getBody());
entities.set(position, tc);
adapter.setEntities(entities);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
srl_container.setRefreshing(false);
adapter.notifyDataSetChanged();
}
});
}
@Override
public void connectFailure(HttpResponse response, Exception e) {
//Running in a non-UI thread right now.
StringBuilder sb = new StringBuilder();
if (!NetworkHelper.isNetworkHealthy()) {
DialogManager.showAlertDialog(getActivity(), "Please turn on Wi-Fi or cellular.", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
sb.append("retry...");
retry = true;
} else {
sb.append(String.format(Locale.ENGLISH, "connectFailure code:%s, message:%s, body:%s", response.getCode(), response.getCodeString(), response.getErrorMessage()));
CLog.e(TAG, sb.toString());
if (e != null) {
sb.append("\n");
sb.append(e.getMessage());
CLog.e(TAG, e.getMessage());
if (e instanceof SocketTimeoutException) {
DialogManager.showAlertDialog(getActivity(), "Connection timeout. Please check your server.", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
}
}
TextCard tc = entities.get(position);
tc.contents = sb.toString();
entities.set(position, tc);
adapter.setEntities(entities);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
srl_container.setRefreshing(false);
adapter.notifyDataSetChanged();
}
});
}
}
@Override
public void onDestroy() {
if (mainInterface != null)
mainInterface.stopListeningToNetworkState(networkHealthListener);
super.onDestroy();
}
}