質問編集履歴

3

追記

2018/09/21 01:11

投稿

Zodiarc
Zodiarc

スコア19

test CHANGED
File without changes
test CHANGED
@@ -144,6 +144,8 @@
144
144
 
145
145
  各Activityから"sendRequestUI"を受け取って共通処理を行い"sendRequest"を呼び出しております。
146
146
 
147
+ Async処理を行っております。
148
+
147
149
  ```
148
150
 
149
151
 

2

ソースコードの添付

2018/09/21 01:11

投稿

Zodiarc
Zodiarc

スコア19

test CHANGED
File without changes
test CHANGED
@@ -48,6 +48,274 @@
48
48
 
49
49
 
50
50
 
51
+ ```ActivationActivity
52
+
53
+ public final Void handleResponse(final HttpResponse response)
54
+
55
+ throws IOException {
56
+
57
+ if ((response == null) || (!Util.isSuccess(response))) {
58
+
59
+ // 通信失敗
60
+
61
+ postMsgBox(getResStr(R.string.errtitle),
62
+
63
+ getResStr(R.string.connection_err));
64
+
65
+ return null;
66
+
67
+ }
68
+
69
+ try {
70
+
71
+ SettingRes res = new SettingRes(response);
72
+
73
+ int status = res.getStatus();
74
+
75
+
76
+
77
+ switch (status) {
78
+
79
+ case Res.OK:
80
+
81
+ 中略
82
+
83
+
84
+
85
+ protected final void onCreate(final Bundle savedInstanceState) {
86
+
87
+ super.onCreate(savedInstanceState);
88
+
89
+ setContentView(R.layout.activity_activation);
90
+
91
+ adjustWindowsSize();
92
+
93
+
94
+
95
+ btnOK = (Button) findViewById(R.id.btnOK);
96
+
97
+ btnCancel = (Button) findViewById(R.id.btnCancel);
98
+
99
+ etAct = (EditText) findViewById(R.id.etAct);
100
+
101
+ etActPass = (EditText) findViewById(R.id.etActPass);
102
+
103
+
104
+
105
+ btnOK.setOnClickListener(new OnClickListener() {
106
+
107
+
108
+
109
+ @Override
110
+
111
+ public void onClick(final View v) {
112
+
113
+ 中略
114
+
115
+ if (Util.getSetting(ActivationActivity.this,
116
+
117
+ TR_SETTING.URL) != null) {
118
+
119
+ actreq.setUrl((String) Util.getSetting(
120
+
121
+ ActivationActivity.this, TR_SETTING.URL));
122
+
123
+ }
124
+
125
+
126
+
127
+ sendRequestUI(actreq, ActivationActivity.this);
128
+
129
+ }
130
+
131
+ });
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+ ```
140
+
141
+
142
+
143
+ ```BaseActivity
144
+
145
+ 各Activityから"sendRequestUI"を受け取って共通処理を行い"sendRequest"を呼び出しております。
146
+
147
+ ```
148
+
149
+
150
+
151
+ ```sendRequest
152
+
153
+ public static void sendRequest(final Req req,
154
+
155
+ final Handler handler) {
156
+
157
+
158
+
159
+ /** 送信用スレッドを定義するためのクラス */
160
+
161
+ class SendThread implements Runnable {
162
+
163
+
164
+
165
+ private final Req mParamReq;
166
+
167
+ private final Handler mHandler;
168
+
169
+
170
+
171
+ /**
172
+
173
+ * コンストラクタ
174
+
175
+ *
176
+
177
+ * @param req
178
+
179
+ * リクエスト
180
+
181
+ * @param handler
182
+
183
+ * レスポンスを処理するハンドラ
184
+
185
+ */
186
+
187
+ public SendThread(final Req req,
188
+
189
+ final Handler handler) {
190
+
191
+ mParamReq = req;
192
+
193
+ mHandler = handler;
194
+
195
+ }
196
+
197
+
198
+
199
+ @Override
200
+
201
+ public void run() {
202
+
203
+ *****************************新規部分*****************************
204
+
205
+ try {
206
+
207
+ final Request request = new Request.Builder()
208
+
209
+ .url(mParamReq.toQueryString())
210
+
211
+ .get()
212
+
213
+ .build();
214
+
215
+ OkHttpClient client = new OkHttpClient();
216
+
217
+ DebugLog.out(mParamReq.toQueryString());
218
+
219
+
220
+
221
+ client.newCall(request).enqueue(new Callback() {
222
+
223
+ public void onResponse(Call call, Response response) throws IOException {
224
+
225
+
226
+
227
+ }
228
+
229
+ public void onFailure(Call call, final IOException e) {
230
+
231
+
232
+
233
+ }
234
+
235
+
236
+
237
+ });
238
+
239
+
240
+
241
+ *****************************新規部分*****************************
242
+
243
+ *****************************既存部分*****************************(削除予定)
244
+
245
+ final int iNTWKTIMEOUT = 3000;
246
+
247
+ try {
248
+
249
+ DefaultHttpClient client = new DefaultHttpClient();
250
+
251
+ HttpParams params = client.getParams();
252
+
253
+ HttpConnectionParams.setConnectionTimeout(params,
254
+
255
+ iNTWKTIMEOUT); // 接続のタイムアウトを3秒にする
256
+
257
+
258
+
259
+ DebugLog.out(mParamReq.toQueryString());
260
+
261
+ HttpGet get = new HttpGet(
262
+
263
+ mParamReq.toQueryString());
264
+
265
+ client.execute(get, mHandler);
266
+
267
+ } catch (ClientProtocolException e) {
268
+
269
+ return;
270
+
271
+ *****************************既存部分*****************************(削除予定)
272
+
273
+ } catch (IOException e) {
274
+
275
+ // ネットワークが繋がらない場合
276
+
277
+ // e.printStackTrace();
278
+
279
+ try {
280
+
281
+ // ネットワークが繋がらない場合、nullのレスポンスを返す
282
+
283
+ mHandler.handleResponse(null);
284
+
285
+ } catch (Exception e1) {
286
+
287
+ return;
288
+
289
+ }
290
+
291
+ }
292
+
293
+ }
294
+
295
+ }
296
+
297
+
298
+
299
+ SendThread mRunnableSender = new SendThread(req, handler);
300
+
301
+ Thread th = new Thread(mRunnableSender, "ReceiveTR");
302
+
303
+
304
+
305
+ th.run();
306
+
307
+ }
308
+
309
+
310
+
311
+
312
+
313
+ ```
314
+
315
+
316
+
317
+
318
+
51
319
 
52
320
 
53
321
  簡易的ですが通信の流れを図にしました。

1

通信の流れを入れ込みました。

2018/09/20 23:44

投稿

Zodiarc
Zodiarc

スコア19

test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,11 @@
45
45
  それともこれ以外のレスポンス系のAPIが必要になるのでしょうか
46
46
 
47
47
  ご教示お願いいたします。
48
+
49
+
50
+
51
+
52
+
53
+ 簡易的ですが通信の流れを図にしました。
54
+
55
+ ![イメージ説明](bcecdb6693921c8ac4c9acb19a9c98d2.png)