質問編集履歴

3

追記

2018/09/21 01:11

投稿

Zodiarc
Zodiarc

スコア19

title CHANGED
File without changes
body CHANGED
@@ -71,6 +71,7 @@
71
71
 
72
72
  ```BaseActivity
73
73
  各Activityから"sendRequestUI"を受け取って共通処理を行い"sendRequest"を呼び出しております。
74
+ Async処理を行っております。
74
75
  ```
75
76
 
76
77
  ```sendRequest

2

ソースコードの添付

2018/09/21 01:11

投稿

Zodiarc
Zodiarc

スコア19

title CHANGED
File without changes
body CHANGED
@@ -23,6 +23,140 @@
23
23
  それともこれ以外のレスポンス系のAPIが必要になるのでしょうか
24
24
  ご教示お願いいたします。
25
25
 
26
+ ```ActivationActivity
27
+ public final Void handleResponse(final HttpResponse response)
28
+ throws IOException {
29
+ if ((response == null) || (!Util.isSuccess(response))) {
30
+ // 通信失敗
31
+ postMsgBox(getResStr(R.string.errtitle),
32
+ getResStr(R.string.connection_err));
33
+ return null;
34
+ }
35
+ try {
36
+ SettingRes res = new SettingRes(response);
37
+ int status = res.getStatus();
26
38
 
39
+ switch (status) {
40
+ case Res.OK:
41
+ 中略
42
+
43
+ protected final void onCreate(final Bundle savedInstanceState) {
44
+ super.onCreate(savedInstanceState);
45
+ setContentView(R.layout.activity_activation);
46
+ adjustWindowsSize();
47
+
48
+ btnOK = (Button) findViewById(R.id.btnOK);
49
+ btnCancel = (Button) findViewById(R.id.btnCancel);
50
+ etAct = (EditText) findViewById(R.id.etAct);
51
+ etActPass = (EditText) findViewById(R.id.etActPass);
52
+
53
+ btnOK.setOnClickListener(new OnClickListener() {
54
+
55
+ @Override
56
+ public void onClick(final View v) {
57
+ 中略
58
+ if (Util.getSetting(ActivationActivity.this,
59
+ TR_SETTING.URL) != null) {
60
+ actreq.setUrl((String) Util.getSetting(
61
+ ActivationActivity.this, TR_SETTING.URL));
62
+ }
63
+
64
+ sendRequestUI(actreq, ActivationActivity.this);
65
+ }
66
+ });
67
+
68
+
69
+
70
+ ```
71
+
72
+ ```BaseActivity
73
+ 各Activityから"sendRequestUI"を受け取って共通処理を行い"sendRequest"を呼び出しております。
74
+ ```
75
+
76
+ ```sendRequest
77
+ public static void sendRequest(final Req req,
78
+ final Handler handler) {
79
+
80
+ /** 送信用スレッドを定義するためのクラス */
81
+ class SendThread implements Runnable {
82
+
83
+ private final Req mParamReq;
84
+ private final Handler mHandler;
85
+
86
+ /**
87
+ * コンストラクタ
88
+ *
89
+ * @param req
90
+ * リクエスト
91
+ * @param handler
92
+ * レスポンスを処理するハンドラ
93
+ */
94
+ public SendThread(final Req req,
95
+ final Handler handler) {
96
+ mParamReq = req;
97
+ mHandler = handler;
98
+ }
99
+
100
+ @Override
101
+ public void run() {
102
+ *****************************新規部分*****************************
103
+ try {
104
+ final Request request = new Request.Builder()
105
+ .url(mParamReq.toQueryString())
106
+ .get()
107
+ .build();
108
+ OkHttpClient client = new OkHttpClient();
109
+ DebugLog.out(mParamReq.toQueryString());
110
+
111
+ client.newCall(request).enqueue(new Callback() {
112
+ public void onResponse(Call call, Response response) throws IOException {
113
+
114
+ }
115
+ public void onFailure(Call call, final IOException e) {
116
+
117
+ }
118
+
119
+ });
120
+
121
+ *****************************新規部分*****************************
122
+ *****************************既存部分*****************************(削除予定)
123
+ final int iNTWKTIMEOUT = 3000;
124
+ try {
125
+ DefaultHttpClient client = new DefaultHttpClient();
126
+ HttpParams params = client.getParams();
127
+ HttpConnectionParams.setConnectionTimeout(params,
128
+ iNTWKTIMEOUT); // 接続のタイムアウトを3秒にする
129
+
130
+ DebugLog.out(mParamReq.toQueryString());
131
+ HttpGet get = new HttpGet(
132
+ mParamReq.toQueryString());
133
+ client.execute(get, mHandler);
134
+ } catch (ClientProtocolException e) {
135
+ return;
136
+ *****************************既存部分*****************************(削除予定)
137
+ } catch (IOException e) {
138
+ // ネットワークが繋がらない場合
139
+ // e.printStackTrace();
140
+ try {
141
+ // ネットワークが繋がらない場合、nullのレスポンスを返す
142
+ mHandler.handleResponse(null);
143
+ } catch (Exception e1) {
144
+ return;
145
+ }
146
+ }
147
+ }
148
+ }
149
+
150
+ SendThread mRunnableSender = new SendThread(req, handler);
151
+ Thread th = new Thread(mRunnableSender, "ReceiveTR");
152
+
153
+ th.run();
154
+ }
155
+
156
+
157
+ ```
158
+
159
+
160
+
27
161
  簡易的ですが通信の流れを図にしました。
28
162
  ![イメージ説明](bcecdb6693921c8ac4c9acb19a9c98d2.png)

1

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

2018/09/20 23:44

投稿

Zodiarc
Zodiarc

スコア19

title CHANGED
File without changes
body CHANGED
@@ -21,4 +21,8 @@
21
21
 
22
22
  これをHttpURLConnectionで表現しようとするとどういった形になりますでしょうか?
23
23
  それともこれ以外のレスポンス系のAPIが必要になるのでしょうか
24
- ご教示お願いいたします。
24
+ ご教示お願いいたします。
25
+
26
+
27
+ 簡易的ですが通信の流れを図にしました。
28
+ ![イメージ説明](bcecdb6693921c8ac4c9acb19a9c98d2.png)