質問編集履歴

1

修正に関してご指摘をいただいたため

2020/06/03 01:19

投稿

yu9718s
yu9718s

スコア9

test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,8 @@
12
12
 
13
13
  ```ここに言語を入力
14
14
 
15
+ import android.annotation.SuppressLint
16
+
15
17
  import android.os.AsyncTask
16
18
 
17
19
  import androidx.appcompat.app.AppCompatActivity
@@ -20,8 +22,6 @@
20
22
 
21
23
  import android.util.Log
22
24
 
23
- import android.widget.TextView
24
-
25
25
  import org.json.JSONObject
26
26
 
27
27
  import java.io.BufferedReader
@@ -36,6 +36,8 @@
36
36
 
37
37
 
38
38
 
39
+ @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
40
+
39
41
  class MainActivity : AppCompatActivity() {
40
42
 
41
43
 
@@ -50,13 +52,119 @@
50
52
 
51
53
 
52
54
 
55
+ val receiver = WeatherInfoReceiver()
56
+
57
+ receiver.execute("130010")
58
+
59
+ }
60
+
61
+ @SuppressLint("StaticFieldLeak")
62
+
63
+ private inner class WeatherInfoReceiver(): AsyncTask<String, String, String>() {
64
+
65
+ override fun doInBackground(vararg params: String?): String {
66
+
67
+ Log.i("MainActivity", "doInBackground")
68
+
69
+
70
+
71
+ val id = params[0]
72
+
73
+ val urlStr = "http://weather.livedoor.com/forecast/webservice/json/v1?city=${id}"
74
+
75
+
76
+
77
+ val url = URL(urlStr)
78
+
79
+ val con = url.openConnection() as HttpURLConnection
80
+
81
+ con.requestMethod = "GET"
82
+
83
+ con.connect()
84
+
85
+ val stream = con.inputStream
86
+
87
+ val result = is2String(stream)
88
+
89
+ con.disconnect()
90
+
91
+ stream.close()
92
+
93
+
94
+
95
+ return result
96
+
97
+ }
98
+
99
+
100
+
101
+ override fun onPostExecute(result: String?) {
102
+
103
+ Log.i("MainActivity", "onPostExecute")
104
+
105
+ val rootJSON = JSONObject(result)
106
+
107
+
108
+
109
+ val forecasts = rootJSON.getJSONArray("forecasts")
110
+
111
+
112
+
113
+ for (i in 0..1) {
114
+
115
+ val forecastsNow = forecasts.getJSONObject(i)
116
+
117
+ Log.i("MainActivity", "$i")
118
+
119
+ val telop = forecastsNow.getString("telop")
120
+
121
+ Log.i("MainActivity", "telop:$telop")
122
+
123
+ //val text1 = findViewById<TextView>(R.id.text1)
124
+
125
+ //text1.text = telop
126
+
127
+
128
+
53
- val dialogFragment = TestDialogFragment()
129
+ val dialogFragment = TestDialogFragment()
130
+
54
-
131
+ val args = Bundle()
132
+
133
+ args.putString("telop", telop)
134
+
135
+ dialogFragment.arguments = args
136
+
55
- dialogFragment.show(supportFragmentManager, "TestDialogFragment")
137
+ dialogFragment.show(supportFragmentManager, "TestDialogFragment")
56
-
138
+
57
- }
139
+ }
140
+
58
-
141
+ }
142
+
59
-
143
+ }
144
+
145
+ private fun is2String(stream: InputStream): String {
146
+
147
+ Log.i("MainActivity", "is2String")
148
+
149
+ val sb = StringBuilder()
150
+
151
+ val reader = BufferedReader(InputStreamReader(stream, "UTF-8"))
152
+
153
+ var line = reader.readLine()
154
+
155
+ while (line != null) {
156
+
157
+ sb.append(line)
158
+
159
+ line = reader.readLine()
160
+
161
+ }
162
+
163
+ reader.close()
164
+
165
+ return sb.toString()
166
+
167
+ }
60
168
 
61
169
  }
62
170
 
@@ -64,41 +172,25 @@
64
172
 
65
173
  ```ここに言語を入力
66
174
 
67
- import android.annotation.SuppressLint
68
-
69
175
  import android.app.AlertDialog
70
176
 
71
177
  import android.app.Dialog
72
178
 
73
- import android.os.AsyncTask
74
-
75
179
  import android.os.Bundle
76
180
 
77
181
  import android.util.Log
78
182
 
79
- import android.widget.TextView
80
-
81
183
  import androidx.fragment.app.DialogFragment
82
184
 
83
- import org.json.JSONObject
185
+
84
-
85
- import java.io.BufferedReader
186
+
86
-
87
- import java.io.InputStream
187
+
88
-
89
- import java.io.InputStreamReader
188
+
90
-
91
- import java.net.HttpURLConnection
189
+
92
-
93
- import java.net.URL
94
-
95
-
96
-
97
- @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
98
190
 
99
191
  class TestDialogFragment : DialogFragment() {
100
192
 
101
- @SuppressLint("ResourceType")
193
+
102
194
 
103
195
  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
104
196
 
@@ -108,13 +200,7 @@
108
200
 
109
201
 
110
202
 
111
- val receiver = WeatherInfoReceiver()
112
-
113
- receiver.execute("130010")
203
+ val telop = arguments?.getString("telop")
114
-
115
-
116
-
117
- builder.setMessage(R.id.text1)
118
204
 
119
205
 
120
206
 
@@ -122,109 +208,7 @@
122
208
 
123
209
  }
124
210
 
125
- @SuppressLint("StaticFieldLeak")
211
+
126
-
127
- private inner class WeatherInfoReceiver(): AsyncTask<String, String, String>() {
128
-
129
- override fun doInBackground(vararg params: String?): String {
130
-
131
- Log.i("MainActivity", "doInBackground")
132
-
133
-
134
-
135
- val id = params[0]
136
-
137
- val urlStr = "http://weather.livedoor.com/forecast/webservice/json/v1?city=${id}"
138
-
139
-
140
-
141
- val url = URL(urlStr)
142
-
143
- val con = url.openConnection() as HttpURLConnection
144
-
145
- con.requestMethod = "GET"
146
-
147
- con.connect()
148
-
149
- val stream = con.inputStream
150
-
151
- val result = is2String(stream)
152
-
153
- con.disconnect()
154
-
155
- stream.close()
156
-
157
-
158
-
159
- return result
160
-
161
- }
162
-
163
-
164
-
165
- override fun onPostExecute(result: String?) {
166
-
167
- Log.i("MainActivity", "onPostExecute")
168
-
169
- val rootJSON = JSONObject(result)
170
-
171
-
172
-
173
- val forecasts = rootJSON.getJSONArray("forecasts")
174
-
175
-
176
-
177
- for (i in 0..1) {
178
-
179
- val forecastsNow = forecasts.getJSONObject(i)
180
-
181
- Log.i("MainActivity", "$i")
182
-
183
- val telop = forecastsNow.getString("telop")
184
-
185
- Log.i("MainActivity", "telop:$telop")
186
-
187
- val today = view?.findViewById<TextView>(R.id.text1)
188
-
189
- today?.text = telop
190
-
191
-
192
-
193
- val builder = AlertDialog.Builder(activity)
194
-
195
- builder.setMessage(telop)
196
-
197
-
198
-
199
- }
200
-
201
- }
202
-
203
- }
204
-
205
- private fun is2String(stream: InputStream): String {
206
-
207
- Log.i("MainActivity", "is2String")
208
-
209
- val sb = StringBuilder()
210
-
211
- val reader = BufferedReader(InputStreamReader(stream, "UTF-8"))
212
-
213
- var line = reader.readLine()
214
-
215
- while (line != null) {
216
-
217
- sb.append(line)
218
-
219
- line = reader.readLine()
220
-
221
- }
222
-
223
- reader.close()
224
-
225
- return sb.toString()
226
-
227
- }
228
212
 
229
213
  }
230
214