質問編集履歴
2
全体表示
test
CHANGED
File without changes
|
test
CHANGED
@@ -148,25 +148,25 @@
|
|
148
148
|
|
149
149
|
|
150
150
|
|
151
|
-
//JSONObjectを使って、まず全体のJSON
|
151
|
+
//JSONObjectを使って、まず全体のJSONArrayを取ります。
|
152
152
|
|
153
153
|
val parentJsonObj = JSONArray(jsonText)
|
154
154
|
|
155
155
|
|
156
156
|
|
157
|
-
//今回のJSONは配列になっているので(データは一つですが)、全体のJSONObjectから、getJSON
|
157
|
+
//今回のJSONは配列になっているので(データは一つですが)、全体のJSONObjectから、getJSONObjで配列0番目の"BTC/JPY"を取ります。
|
158
158
|
|
159
159
|
val parentJSONObject = parentJsonObj.getJSONObject(0)
|
160
160
|
|
161
161
|
|
162
162
|
|
163
|
-
//
|
163
|
+
//Product_codeのデータのtitle項目をStringで取ります。これで中身を取れました。
|
164
164
|
|
165
165
|
val product_code: String = parentJSONObject.getString("product_code") // => Your Name.
|
166
166
|
|
167
167
|
|
168
168
|
|
169
|
-
//
|
169
|
+
//product_codeでreturnしてあげましょう。
|
170
170
|
|
171
171
|
return "$product_code" // => Your Name. - 2016
|
172
172
|
|
@@ -234,6 +234,40 @@
|
|
234
234
|
|
235
235
|
|
236
236
|
|
237
|
+
```
|
238
|
+
|
239
|
+
レスポンス
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
[
|
244
|
+
|
245
|
+
{ "product_code": "BTC_JPY" },
|
246
|
+
|
247
|
+
{ "product_code": "FX_BTC_JPY" },
|
248
|
+
|
249
|
+
{ "product_code": "ETH_BTC" },
|
250
|
+
|
251
|
+
{
|
252
|
+
|
253
|
+
"product_code": "BTCJPY28APR2017",
|
254
|
+
|
255
|
+
"alias": "BTCJPY_MAT1WK"
|
256
|
+
|
257
|
+
},
|
258
|
+
|
259
|
+
{
|
260
|
+
|
261
|
+
"product_code": "BTCJPY05MAY2017",
|
262
|
+
|
263
|
+
"alias": "BTCJPY_MAT2WK"
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
]
|
268
|
+
|
269
|
+
```
|
270
|
+
|
237
271
|
|
238
272
|
|
239
273
|
全体像はこうなっております。
|
1
全文表記
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,6 +14,42 @@
|
|
14
14
|
|
15
15
|
```java
|
16
16
|
|
17
|
+
|
18
|
+
|
19
|
+
MainActivity.java
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
import android.os.AsyncTask
|
24
|
+
|
25
|
+
import android.support.v7.app.AppCompatActivity
|
26
|
+
|
27
|
+
import android.os.Bundle
|
28
|
+
|
29
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
30
|
+
|
31
|
+
import org.json.JSONArray
|
32
|
+
|
33
|
+
import org.json.JSONException
|
34
|
+
|
35
|
+
import org.json.JSONObject
|
36
|
+
|
37
|
+
import java.io.BufferedReader
|
38
|
+
|
39
|
+
import java.io.IOException
|
40
|
+
|
41
|
+
import java.io.InputStreamReader
|
42
|
+
|
43
|
+
import java.net.HttpURLConnection
|
44
|
+
|
45
|
+
import java.net.MalformedURLException
|
46
|
+
|
47
|
+
import java.net.URL
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
17
53
|
class MainActivity : AppCompatActivity() {
|
18
54
|
|
19
55
|
|
@@ -30,7 +66,7 @@
|
|
30
66
|
|
31
67
|
//ボタンがクリックされたらAPIを叩く。
|
32
68
|
|
33
|
-
HitAPITask().execute("
|
69
|
+
HitAPITask().execute("https://api.bitflyer.jp/v1/getmarkets")
|
34
70
|
|
35
71
|
}
|
36
72
|
|
@@ -38,12 +74,172 @@
|
|
38
74
|
|
39
75
|
|
40
76
|
|
77
|
+
//後からここにinner classを作ります。
|
78
|
+
|
41
|
-
inner class HitAPITask: Asyn
|
79
|
+
inner class HitAPITask: AsyncTask<String, String, String>(){
|
80
|
+
|
81
|
+
|
82
|
+
|
42
|
-
|
83
|
+
override fun doInBackground(vararg params: String?): String? {
|
84
|
+
|
85
|
+
//ここでAPIを叩きます。バックグラウンドで処理する内容です。
|
86
|
+
|
87
|
+
var connection: HttpURLConnection? = null
|
88
|
+
|
89
|
+
var reader: BufferedReader? = null
|
90
|
+
|
91
|
+
val buffer: StringBuffer
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
try {
|
96
|
+
|
97
|
+
//param[0]にはAPIのURI(String)を入れます(後ほど)。
|
98
|
+
|
99
|
+
//AsynkTask<...>の一つめがStringな理由はURIをStringで指定するからです。
|
100
|
+
|
101
|
+
val url = URL(params[0])
|
102
|
+
|
103
|
+
connection = url.openConnection() as HttpURLConnection
|
104
|
+
|
105
|
+
connection.connect() //ここで指定したAPIを叩いてみてます。
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
//ここから叩いたAPIから帰ってきたデータを使えるよう処理していきます。
|
110
|
+
|
111
|
+
|
112
|
+
|
43
|
-
//
|
113
|
+
//とりあえず取得した文字をbufferに。
|
114
|
+
|
115
|
+
val stream = connection.inputStream
|
116
|
+
|
117
|
+
reader = BufferedReader(InputStreamReader(stream))
|
118
|
+
|
119
|
+
buffer = StringBuffer()
|
120
|
+
|
121
|
+
var line: String?
|
122
|
+
|
123
|
+
while (true) {
|
124
|
+
|
125
|
+
line = reader.readLine()
|
126
|
+
|
127
|
+
if (line == null) {
|
128
|
+
|
129
|
+
break
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
buffer.append(line)
|
134
|
+
|
135
|
+
//Log.d("CHECK", buffer.toString())
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
//ここからは、今回はJSONなので、いわゆるJsonをParseする作業(Jsonの中の一つ一つのデータを取るような感じ)をしていきます。
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
//先ほどbufferに入れた、取得した文字列
|
146
|
+
|
147
|
+
val jsonText = buffer.toString()
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
//JSONObjectを使って、まず全体のJSONObjectを取ります。
|
152
|
+
|
153
|
+
val parentJsonObj = JSONArray(jsonText)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
//今回のJSONは配列になっているので(データは一つですが)、全体のJSONObjectから、getJSONArrayで配列0番目の"BTC/JPY"を取ります。
|
158
|
+
|
159
|
+
val parentJSONObject = parentJsonObj.getJSONObject(0)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
//0番目のデータのtitle項目をStringで取ります。これで中身を取れました。
|
164
|
+
|
165
|
+
val product_code: String = parentJSONObject.getString("product_code") // => Your Name.
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
//Stringでreturnしてあげましょう。
|
170
|
+
|
171
|
+
return "$product_code" // => Your Name. - 2016
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
//ここから下は、接続エラーとかJSONのエラーとかで失敗した時にエラーを処理する為のものです。
|
176
|
+
|
177
|
+
} catch (e: MalformedURLException) {
|
178
|
+
|
179
|
+
e.printStackTrace()
|
180
|
+
|
181
|
+
} catch (e: IOException) {
|
182
|
+
|
183
|
+
e.printStackTrace()
|
184
|
+
|
185
|
+
} catch (e: JSONException) {
|
186
|
+
|
187
|
+
e.printStackTrace()
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
//finallyで接続を切断してあげましょう。
|
192
|
+
|
193
|
+
finally {
|
194
|
+
|
195
|
+
connection?.disconnect()
|
196
|
+
|
197
|
+
try {
|
198
|
+
|
199
|
+
reader?.close()
|
200
|
+
|
201
|
+
} catch (e: IOException) {
|
202
|
+
|
203
|
+
e.printStackTrace()
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
//失敗した時はnullやエラーコードなどを返しましょう。
|
210
|
+
|
211
|
+
return null
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
//返ってきたデータをビューに反映させる処理はonPostExecuteに書きます。これはメインスレッドです。
|
216
|
+
|
217
|
+
override fun onPostExecute(result: String?) {
|
218
|
+
|
219
|
+
super.onPostExecute(result)
|
220
|
+
|
221
|
+
if(result == null) return
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
textView.text = result
|
226
|
+
|
227
|
+
}
|
44
228
|
|
45
229
|
}
|
46
230
|
|
47
231
|
}
|
48
232
|
|
49
233
|
```
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
全体像はこうなっております。
|
240
|
+
|
241
|
+
OnClickListenerを消すと
|
242
|
+
|
243
|
+
その直後の`inner`にエラーが出てしまいます。。
|
244
|
+
|
245
|
+
(Modifier 'liner' is not applicable inside 'file')
|