質問編集履歴
2
記述方法が適切ではなかったため
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,540 +1,376 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
########################################################
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
前提・実現したいこと
|
6
|
+
|
7
|
+
Http通信を用いてリクエストを送信したページのXMLかJSONをアンドロイド上に受け取り表示がしたいです
|
8
|
+
|
9
|
+
最終的にはボタンを押すとオープンストリートマップにリクエストを送信し地図上のデータを取得することが目的です。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
・ボタンを押すとアプリは落ちずエラーはおきませんがnullが返ってくるようになりました。
|
14
|
+
|
15
|
+
・UnsupportedEncodingExceptionの部分でThere is more general expection,,in the throwsList alredyがでるようになりました。
|
16
|
+
|
17
|
+
・java.io.FileNotFoundException:というエラーが返ってきます
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
解決するためにどうすればよいか教えて頂ければ幸いです
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
###エラーメッセージ
|
28
|
+
|
29
|
+
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
java.io.FileNotFoundException: http://api.openstreetmap.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
at com.example.ichita_miura.httptest6.HttpResponsAsync.doInBackground(HttpResponsAsync.java:56)
|
46
|
+
|
47
|
+
at android.os.AsyncTask$2.call(AsyncTask.java:305)
|
48
|
+
|
49
|
+
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
|
50
|
+
|
51
|
+
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
|
52
|
+
|
53
|
+
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
|
54
|
+
|
55
|
+
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
|
56
|
+
|
57
|
+
at java.lang.Thread.run(Thread.java:761)
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
###コード
|
62
|
+
|
63
|
+
MainActivity
|
64
|
+
|
65
|
+
```
|
4
66
|
|
5
67
|
package com.example.ichita_miura.httptest6;
|
6
68
|
|
7
69
|
|
8
70
|
|
71
|
+
import android.support.v7.app.AppCompatActivity;
|
72
|
+
|
73
|
+
import android.os.Bundle;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
import android.view.View;
|
78
|
+
|
79
|
+
import android.widget.TextView;
|
80
|
+
|
81
|
+
import android.widget.Button;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
public class MainActivity extends AppCompatActivity {
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
private TextView textView;
|
94
|
+
|
95
|
+
private Button buttonStart, buttonClear;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
@Override
|
100
|
+
|
101
|
+
protected void onCreate(Bundle savedInstanceState) {
|
102
|
+
|
103
|
+
super.onCreate(savedInstanceState);
|
104
|
+
|
105
|
+
setContentView(R.layout.activity_main);
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
textView =(TextView)findViewById(R.id.text_view);
|
110
|
+
|
111
|
+
buttonStart = (Button)findViewById(R.id.button_start);
|
112
|
+
|
113
|
+
buttonStart.setOnClickListener(new View.OnClickListener(){
|
114
|
+
|
115
|
+
@Override
|
116
|
+
|
117
|
+
public void onClick(View view){
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
startAsync();
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
});
|
126
|
+
|
127
|
+
buttonClear = (Button)findViewById(R.id.button_clear);
|
128
|
+
|
129
|
+
buttonClear.setOnClickListener(new View.OnClickListener(){
|
130
|
+
|
131
|
+
@Override
|
132
|
+
|
133
|
+
public void onClick(View view){
|
134
|
+
|
135
|
+
setTextView(String.valueOf(0));
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
});
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
private void startAsync(){
|
144
|
+
|
145
|
+
HttpResponsAsync httpResponsAsync = new HttpResponsAsync(this);
|
146
|
+
|
147
|
+
httpResponsAsync.execute();
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
public void setTextView(String testout){
|
152
|
+
|
153
|
+
textView.setText(String.valueOf(testout));
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
コード
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
AsyncTask
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
package com.example.ichita_miura.httptest6;
|
168
|
+
|
169
|
+
|
170
|
+
|
9
171
|
import android.os.AsyncTask;
|
10
172
|
|
11
173
|
|
12
174
|
|
175
|
+
|
176
|
+
|
13
|
-
import java.io.BufferedReader;
|
177
|
+
import java.io.BufferedReader;
|
14
|
-
|
178
|
+
|
15
|
-
import java.io.IOException;
|
179
|
+
import java.io.IOException;
|
16
|
-
|
180
|
+
|
17
|
-
import java.io.InputStream;
|
181
|
+
import java.io.InputStream;
|
18
|
-
|
182
|
+
|
19
|
-
import java.io.InputStreamReader;
|
183
|
+
import java.io.InputStreamReader;
|
20
|
-
|
184
|
+
|
21
|
-
import java.io.UnsupportedEncodingException;
|
185
|
+
import java.io.UnsupportedEncodingException;
|
22
|
-
|
186
|
+
|
23
|
-
import java.net.HttpURLConnection;
|
187
|
+
import java.net.HttpURLConnection;
|
24
|
-
|
188
|
+
|
25
|
-
import java.net.MalformedURLException;
|
189
|
+
import java.net.MalformedURLException;
|
26
190
|
|
27
191
|
import java.net.URL;
|
28
192
|
|
29
193
|
|
30
194
|
|
31
|
-
import org.json.JSONException;
|
195
|
+
import org.json.JSONException;
|
32
196
|
|
33
197
|
import org.json.JSONObject;
|
34
198
|
|
35
199
|
|
36
200
|
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
37
205
|
public class HttpResponsAsync extends AsyncTask<Void, Void, String> {
|
38
206
|
|
39
207
|
|
40
208
|
|
41
|
-
private MainActivity mainActivity;
|
209
|
+
private MainActivity mainActivity;
|
42
|
-
|
43
|
-
|
44
|
-
|
210
|
+
|
211
|
+
|
212
|
+
|
45
|
-
public HttpResponsAsync(MainActivity activity){
|
213
|
+
public HttpResponsAsync(MainActivity activity){
|
46
|
-
|
214
|
+
|
47
|
-
mainActivity = activity;
|
215
|
+
mainActivity = activity;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
@Override
|
222
|
+
|
223
|
+
protected void onPreExecute() {
|
224
|
+
|
225
|
+
super.onPreExecute();
|
226
|
+
|
227
|
+
// doInBackground前処理
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
@Override
|
234
|
+
|
235
|
+
protected String doInBackground(Void... params) {
|
236
|
+
|
237
|
+
HttpURLConnection con = null;
|
238
|
+
|
239
|
+
URL url = null;
|
240
|
+
|
241
|
+
String urlst = "https://www.googleapis.com/books/v1/volumes?q=android";
|
242
|
+
|
243
|
+
//String urlst ="http://api.openstreetmap.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145";
|
244
|
+
|
245
|
+
try {
|
246
|
+
|
247
|
+
url = new URL(urlst);
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
con = (HttpURLConnection)url.openConnection();
|
252
|
+
|
253
|
+
// リクエストメソッドの設定
|
254
|
+
|
255
|
+
con.setRequestMethod("POST");
|
256
|
+
|
257
|
+
// リダイレクトを自動で許可しない設定
|
258
|
+
|
259
|
+
con.setInstanceFollowRedirects(false);
|
260
|
+
|
261
|
+
// URL接続からデータを読み取る場合はtrue
|
262
|
+
|
263
|
+
con.setDoInput(true);
|
264
|
+
|
265
|
+
// URL接続にデータを書き込む場合はtrue
|
266
|
+
|
267
|
+
con.setDoOutput(true);
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
// 接続
|
272
|
+
|
273
|
+
con.connect(); // ①
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
InputStream in = con.getInputStream();
|
278
|
+
|
279
|
+
byte bodyByte[] = new byte[1024];
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
String readSt = readInputStream(in);
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
//JSONObject jsonData = new JSONObject(readSt).getJSONObject("オブジェクト名");
|
288
|
+
|
289
|
+
//JSONArray jsonArray = new JSONObject(readSt).getJSONArray("オブジェクト名");
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
//String St = jsonData.getString("オブジェクト名");
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
in.read(bodyByte);
|
298
|
+
|
299
|
+
in.close();
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
} catch (MalformedURLException e) {
|
304
|
+
|
305
|
+
e.printStackTrace();
|
306
|
+
|
307
|
+
} catch (IOException e) {
|
308
|
+
|
309
|
+
e.printStackTrace();
|
310
|
+
|
311
|
+
} //catch (JSONException e){
|
312
|
+
|
313
|
+
//e.printStackTrace();
|
314
|
+
|
315
|
+
// }
|
316
|
+
|
317
|
+
return null;
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
@Override
|
324
|
+
|
325
|
+
protected void onPostExecute(String result) {
|
326
|
+
|
327
|
+
mainActivity.setTextView(result);
|
328
|
+
|
329
|
+
super.onPostExecute(result);
|
330
|
+
|
331
|
+
// doInBackground後処理
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
public String readInputStream(InputStream in)throws IOException, UnsupportedEncodingException{
|
336
|
+
|
337
|
+
StringBuffer sb = new StringBuffer();
|
338
|
+
|
339
|
+
String st = "";
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
|
344
|
+
|
345
|
+
while ((st = br.readLine()) != null)
|
346
|
+
|
347
|
+
{
|
348
|
+
|
349
|
+
sb.append(st);
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
try {
|
354
|
+
|
355
|
+
in.close();
|
356
|
+
|
357
|
+
}catch (Exception e){
|
358
|
+
|
359
|
+
e.printStackTrace();
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
return sb.toString();
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
48
368
|
|
49
369
|
}
|
50
370
|
|
51
371
|
|
52
372
|
|
53
|
-
|
54
|
-
|
55
|
-
protected void onPreExecute() {
|
56
|
-
|
57
|
-
super.onPreExecute();
|
58
|
-
|
59
|
-
// doInBackground前処理
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
@Override
|
66
|
-
|
67
|
-
protected String doInBackground(Void... params) {
|
68
|
-
|
69
|
-
HttpURLConnection con = null;
|
70
|
-
|
71
|
-
URL url = null;
|
72
|
-
|
73
|
-
String urlst = "https://www.googleapis.com/books/v1/volumes?q=android";
|
74
|
-
|
75
|
-
try {
|
76
|
-
|
77
|
-
url = new URL(urlst);
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
con = (HttpURLConnection)url.openConnection();
|
82
|
-
|
83
|
-
// リクエストメソッドの設定
|
84
|
-
|
85
|
-
con.setRequestMethod("POST");
|
86
|
-
|
87
|
-
// リダイレクトを自動で許可しない設定
|
88
|
-
|
89
|
-
con.setInstanceFollowRedirects(false);
|
90
|
-
|
91
|
-
// URL接続からデータを読み取る場合はtrue
|
92
|
-
|
93
|
-
con.setDoInput(true);
|
94
|
-
|
95
|
-
// URL接続にデータを書き込む場合はtrue
|
96
|
-
|
97
|
-
con.setDoOutput(true);
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
// 接続
|
102
|
-
|
103
|
-
con.connect(); // ①
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
InputStream in = con.getInputStream();
|
108
|
-
|
109
|
-
byte bodyByte[] = new byte[1024];
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
String readSt = readInputStream(in);
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
JSONObject jsonData = new JSONObject(readSt).getJSONObject("オブジェクト名");
|
118
|
-
|
119
|
-
//JSONArray jsonArray = new JSONObject(readSt).getJSONArray("オブジェクト名");
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
//String St = jsonData.getString("オブジェクト名");
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
in.read(bodyByte);
|
128
|
-
|
129
|
-
in.close();
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
} catch (MalformedURLException e) {
|
134
|
-
|
135
|
-
e.printStackTrace();
|
136
|
-
|
137
|
-
} catch (IOException e) {
|
138
|
-
|
139
|
-
e.printStackTrace();
|
140
|
-
|
141
|
-
} catch (JSONException e){
|
142
|
-
|
143
|
-
e.printStackTrace();
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
return null;
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
@Override
|
154
|
-
|
155
|
-
protected void onPostExecute(String result) {
|
156
|
-
|
157
|
-
mainActivity.setTextView(result);
|
158
|
-
|
159
|
-
super.onPostExecute(result);
|
160
|
-
|
161
|
-
// doInBackground後処理
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
public String readInputStream(InputStream in)throws IOException, UnsupportedEncodingException{
|
166
|
-
|
167
|
-
StringBuffer sb = new StringBuffer();
|
168
|
-
|
169
|
-
String st = "";
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
|
174
|
-
|
175
|
-
while ((st = br.readLine()) != null)
|
176
|
-
|
177
|
-
{
|
178
|
-
|
179
|
-
sb.append(st);
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
try {
|
184
|
-
|
185
|
-
in.close();
|
186
|
-
|
187
|
-
}catch (Exception e){
|
188
|
-
|
189
|
-
e.printStackTrace();
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
return sb.toString();
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
}
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
前回
|
204
|
-
|
205
|
-
########################################################
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
###前提・実現したいこと#########
|
210
|
-
|
211
|
-
Http通信を用いてリクエストを送信したページのXMLかJSONをアンドロイド上に受け取り表示がしたいです
|
212
|
-
|
213
|
-
最終的にはボタンを押すとオープンストリートマップにリクエストを送信し地図上のデータを取得することが目的です
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
現状アプリまでは起動しますがボタンを押すとアプリが強制終了します
|
218
|
-
|
219
|
-
###発生している問題・エラーメッセージ###########
|
220
|
-
|
221
|
-
E/AndroidRuntime: FATAL EXCEPTION: main
|
222
|
-
|
223
|
-
Process: com.example.ichita_miura.httptest6, PID: 2653
|
224
|
-
|
225
|
-
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ichita_miura.httptest6.MainActivity.setTextView(java.lang.String)' on a null object reference
|
226
|
-
|
227
|
-
at com.example.ichita_miura.httptest6.HttpResponsAsync.onPostExecute(HttpResponsAsync.java:81)
|
228
|
-
|
229
|
-
at com.example.ichita_miura.httptest6.HttpResponsAsync.onPostExecute(HttpResponsAsync.java:25)
|
230
|
-
|
231
|
-
at android.os.AsyncTask.finish(AsyncTask.java:667)
|
232
|
-
|
233
|
-
at android.os.AsyncTask.-wrap1(AsyncTask.java)
|
234
|
-
|
235
|
-
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
|
236
|
-
|
237
|
-
at android.os.Handler.dispatchMessage(Handler.java:102)
|
238
|
-
|
239
|
-
at android.os.Looper.loop(Looper.java:154)
|
240
|
-
|
241
|
-
at android.app.ActivityThread.main(ActivityThread.java:6119)
|
242
|
-
|
243
|
-
at java.lang.reflect.Method.invoke(Native Method)
|
244
|
-
|
245
|
-
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
|
246
|
-
|
247
|
-
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
###該当のソースコード#################
|
252
|
-
|
253
|
-
Main activity
|
254
|
-
|
255
|
-
####################################
|
256
|
-
|
257
|
-
package com.example.ichita_miura.httptest6;
|
258
|
-
|
259
|
-
import android.support.v7.app.AppCompatActivity;
|
260
|
-
|
261
|
-
import android.os.Bundle;
|
262
|
-
|
263
|
-
import android.view.View;
|
264
|
-
|
265
|
-
import android.widget.TextView;
|
266
|
-
|
267
|
-
import android.widget.Button;
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
public class MainActivity extends AppCompatActivity {
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
private TextView textView;
|
276
|
-
|
277
|
-
private Button buttonStart, buttonClear;
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
@Override
|
282
|
-
|
283
|
-
protected void onCreate(Bundle savedInstanceState) {
|
284
|
-
|
285
|
-
super.onCreate(savedInstanceState);
|
286
|
-
|
287
|
-
setContentView(R.layout.activity_main);
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
textView =(TextView)findViewById(R.id.text_view);
|
292
|
-
|
293
|
-
buttonStart = (Button)findViewById(R.id.button_start);
|
294
|
-
|
295
|
-
buttonStart.setOnClickListener(new View.OnClickListener(){
|
296
|
-
|
297
|
-
@Override
|
298
|
-
|
299
|
-
public void onClick(View view){
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
startAsync();
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
});
|
308
|
-
|
309
|
-
buttonClear = (Button)findViewById(R.id.button_clear);
|
310
|
-
|
311
|
-
buttonClear.setOnClickListener(new View.OnClickListener(){
|
312
|
-
|
313
|
-
@Override
|
314
|
-
|
315
|
-
public void onClick(View view){
|
316
|
-
|
317
|
-
setTextView(String.valueOf(0));
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
});
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
private void startAsync(){
|
326
|
-
|
327
|
-
HttpResponsAsync httpResponsAsync = new HttpResponsAsync();
|
328
|
-
|
329
|
-
httpResponsAsync.execute();
|
330
|
-
|
331
|
-
}
|
332
|
-
|
333
|
-
void setTextView(String testout){
|
334
|
-
|
335
|
-
textView.setText(String.valueOf(testout));
|
336
|
-
|
337
|
-
}
|
338
|
-
|
339
|
-
}
|
340
|
-
|
341
|
-
=========================================================
|
342
|
-
|
343
|
-
通信の処理部分
|
344
|
-
|
345
|
-
=========================================================
|
346
|
-
|
347
|
-
package com.example.ichita_miura.httptest6;
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
import android.os.AsyncTask;
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
import java.io.BufferedReader;
|
356
|
-
|
357
|
-
import java.io.IOException;
|
358
|
-
|
359
|
-
import java.io.InputStream;
|
360
|
-
|
361
|
-
import java.io.InputStreamReader;
|
362
|
-
|
363
|
-
import java.io.UnsupportedEncodingException;
|
364
|
-
|
365
|
-
import java.net.HttpURLConnection;
|
366
|
-
|
367
|
-
import java.net.MalformedURLException;
|
368
|
-
|
369
|
-
import java.net.URL;
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
import org.json.JSONException;
|
374
|
-
|
375
|
-
import org.json.JSONObject;
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
public class HttpResponsAsync extends AsyncTask<Void, Void, String> {
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
private MainActivity mainActivity;
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
@Override
|
392
|
-
|
393
|
-
protected void onPreExecute() {
|
394
|
-
|
395
|
-
super.onPreExecute();
|
396
|
-
|
397
|
-
// doInBackground前処理
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
@Override
|
404
|
-
|
405
|
-
protected String doInBackground(Void... params) {
|
406
|
-
|
407
|
-
HttpURLConnection con = null;
|
408
|
-
|
409
|
-
URL url = null;
|
410
|
-
|
411
|
-
String urlst = "https://www.googleapis.com/books/v1/volumes?q=android";
|
412
|
-
|
413
|
-
try {
|
414
|
-
|
415
|
-
url = new URL(urlst);
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
con = (HttpURLConnection)url.openConnection();
|
420
|
-
|
421
|
-
// リクエストメソッドの設定
|
422
|
-
|
423
|
-
con.setRequestMethod("POST");
|
424
|
-
|
425
|
-
// リダイレクトを自動で許可しない設定
|
426
|
-
|
427
|
-
con.setInstanceFollowRedirects(false);
|
428
|
-
|
429
|
-
// URL接続からデータを読み取る場合はtrue
|
430
|
-
|
431
|
-
con.setDoInput(true);
|
432
|
-
|
433
|
-
// URL接続にデータを書き込む場合はtrue
|
434
|
-
|
435
|
-
con.setDoOutput(true);
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
// 接続
|
440
|
-
|
441
|
-
con.connect(); // ①
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
InputStream in = con.getInputStream();
|
446
|
-
|
447
|
-
byte bodyByte[] = new byte[1024];
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
String readSt = readInputStream(in);
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
JSONObject jsonData = new JSONObject(readSt).getJSONObject("オブジェクト名");
|
456
|
-
|
457
|
-
//JSONArray jsonArray = new JSONObject(readSt).getJSONArray("オブジェクト名");
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
//String St = jsonData.getString("オブジェクト名");
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
in.read(bodyByte);
|
466
|
-
|
467
|
-
in.close();
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
} catch (MalformedURLException e) {
|
472
|
-
|
473
|
-
e.printStackTrace();
|
474
|
-
|
475
|
-
} catch (IOException e) {
|
476
|
-
|
477
|
-
e.printStackTrace();
|
478
|
-
|
479
|
-
} catch (JSONException e){
|
480
|
-
|
481
|
-
e.printStackTrace();
|
482
|
-
|
483
|
-
}
|
484
|
-
|
485
|
-
return null;
|
486
|
-
|
487
|
-
}
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
@Override
|
492
|
-
|
493
|
-
protected void onPostExecute(String result) {
|
494
|
-
|
495
|
-
mainActivity.setTextView(result);
|
496
|
-
|
497
|
-
super.onPostExecute(result);
|
498
|
-
|
499
|
-
// doInBackground後処理
|
500
|
-
|
501
|
-
}
|
502
|
-
|
503
|
-
public String readInputStream(InputStream in)throws IOException, UnsupportedEncodingException{
|
504
|
-
|
505
|
-
StringBuffer sb = new StringBuffer();
|
506
|
-
|
507
|
-
String st = "";
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
|
512
|
-
|
513
|
-
while ((st = br.readLine()) != null)
|
514
|
-
|
515
|
-
{
|
516
|
-
|
517
|
-
sb.append(st);
|
518
|
-
|
519
|
-
}
|
520
|
-
|
521
|
-
try {
|
522
|
-
|
523
|
-
in.close();
|
524
|
-
|
525
|
-
}catch (Exception e){
|
526
|
-
|
527
|
-
e.printStackTrace();
|
528
|
-
|
529
|
-
}
|
530
|
-
|
531
|
-
return sb.toString();
|
532
|
-
|
533
|
-
}
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
}
|
373
|
+
```
|
538
374
|
|
539
375
|
|
540
376
|
|
1
プログラム更新し、アプリは落ちないがnullが返ってくるように
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,211 @@
|
|
1
|
+
このコードに変更後ボタンを押すとアプリは落ちずエラーはおきませんがnullが返ってくるようになりましたUnsupportedEncodingExceptionの部分でThere is more general expection,,in the throwsList alredyがでるようになりましたこの部分の解決方法を知りたいのですが。。。ご回答いただければ幸いです
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
package com.example.ichita_miura.httptest6;
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
import android.os.AsyncTask;
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
import java.io.BufferedReader;
|
14
|
+
|
15
|
+
import java.io.IOException;
|
16
|
+
|
17
|
+
import java.io.InputStream;
|
18
|
+
|
19
|
+
import java.io.InputStreamReader;
|
20
|
+
|
21
|
+
import java.io.UnsupportedEncodingException;
|
22
|
+
|
23
|
+
import java.net.HttpURLConnection;
|
24
|
+
|
25
|
+
import java.net.MalformedURLException;
|
26
|
+
|
27
|
+
import java.net.URL;
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
import org.json.JSONException;
|
32
|
+
|
33
|
+
import org.json.JSONObject;
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
public class HttpResponsAsync extends AsyncTask<Void, Void, String> {
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
private MainActivity mainActivity;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
public HttpResponsAsync(MainActivity activity){
|
46
|
+
|
47
|
+
mainActivity = activity;
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
@Override
|
54
|
+
|
55
|
+
protected void onPreExecute() {
|
56
|
+
|
57
|
+
super.onPreExecute();
|
58
|
+
|
59
|
+
// doInBackground前処理
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
@Override
|
66
|
+
|
67
|
+
protected String doInBackground(Void... params) {
|
68
|
+
|
69
|
+
HttpURLConnection con = null;
|
70
|
+
|
71
|
+
URL url = null;
|
72
|
+
|
73
|
+
String urlst = "https://www.googleapis.com/books/v1/volumes?q=android";
|
74
|
+
|
75
|
+
try {
|
76
|
+
|
77
|
+
url = new URL(urlst);
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
con = (HttpURLConnection)url.openConnection();
|
82
|
+
|
83
|
+
// リクエストメソッドの設定
|
84
|
+
|
85
|
+
con.setRequestMethod("POST");
|
86
|
+
|
87
|
+
// リダイレクトを自動で許可しない設定
|
88
|
+
|
89
|
+
con.setInstanceFollowRedirects(false);
|
90
|
+
|
91
|
+
// URL接続からデータを読み取る場合はtrue
|
92
|
+
|
93
|
+
con.setDoInput(true);
|
94
|
+
|
95
|
+
// URL接続にデータを書き込む場合はtrue
|
96
|
+
|
97
|
+
con.setDoOutput(true);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
// 接続
|
102
|
+
|
103
|
+
con.connect(); // ①
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
InputStream in = con.getInputStream();
|
108
|
+
|
109
|
+
byte bodyByte[] = new byte[1024];
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
String readSt = readInputStream(in);
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
JSONObject jsonData = new JSONObject(readSt).getJSONObject("オブジェクト名");
|
118
|
+
|
119
|
+
//JSONArray jsonArray = new JSONObject(readSt).getJSONArray("オブジェクト名");
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
//String St = jsonData.getString("オブジェクト名");
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
in.read(bodyByte);
|
128
|
+
|
129
|
+
in.close();
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
} catch (MalformedURLException e) {
|
134
|
+
|
135
|
+
e.printStackTrace();
|
136
|
+
|
137
|
+
} catch (IOException e) {
|
138
|
+
|
139
|
+
e.printStackTrace();
|
140
|
+
|
141
|
+
} catch (JSONException e){
|
142
|
+
|
143
|
+
e.printStackTrace();
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
return null;
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
@Override
|
154
|
+
|
155
|
+
protected void onPostExecute(String result) {
|
156
|
+
|
157
|
+
mainActivity.setTextView(result);
|
158
|
+
|
159
|
+
super.onPostExecute(result);
|
160
|
+
|
161
|
+
// doInBackground後処理
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
public String readInputStream(InputStream in)throws IOException, UnsupportedEncodingException{
|
166
|
+
|
167
|
+
StringBuffer sb = new StringBuffer();
|
168
|
+
|
169
|
+
String st = "";
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
|
174
|
+
|
175
|
+
while ((st = br.readLine()) != null)
|
176
|
+
|
177
|
+
{
|
178
|
+
|
179
|
+
sb.append(st);
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
try {
|
184
|
+
|
185
|
+
in.close();
|
186
|
+
|
187
|
+
}catch (Exception e){
|
188
|
+
|
189
|
+
e.printStackTrace();
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
return sb.toString();
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
前回
|
204
|
+
|
205
|
+
########################################################
|
206
|
+
|
207
|
+
|
208
|
+
|
1
209
|
###前提・実現したいこと#########
|
2
210
|
|
3
211
|
Http通信を用いてリクエストを送信したページのXMLかJSONをアンドロイド上に受け取り表示がしたいです
|