OpenWeatherMapAPIからjsonを取得してTextViewに反映させたいのですが、実行しても反映されていないです。
恥ずかしながら初心者なので、一から教えていただくと嬉しいです。。
ご力添えお待ちしてます。
下のJSONの"weather"の中の"main"の文字をそのままTextViewに反映したいです。
Json
1{ 2 "coord": { 3 "lon": 145.77, 4 "lat": -16.92 5 }, 6 "weather": [ 7 { 8 "id": 802, 9 "main": "Clouds", 10 "description": "scattered clouds", 11 "icon": "03n" 12 } 13 ], 14 "base": "stations", 15 "main": { 16 "temp": 300.15, 17 "pressure": 1007, 18 "humidity": 74, 19 "temp_min": 300.15, 20 "temp_max": 300.15 21 }, 22 "visibility": 10000, 23 "wind": { 24 "speed": 3.6, 25 "deg": 160 26 }, 27 "clouds": { 28 "all": 40 29 }, 30 "dt": 1485790200, 31 "sys": { 32 "type": 1, 33 "id": 8166, 34 "message": 0.2064, 35 "country": "AU", 36 "sunrise": 1485720272, 37 "sunset": 1485766550 38 }, 39 "id": 2172797, 40 "name": "Cairns", 41 "cod": 200 42}
Java
1 2public class MapsActivity extends AppCompatActivity implements 3 OnMapReadyCallback,ActivityCompat.OnRequestPermissionsResultCallback, LocationListener { 4 5 TextView tenki_text; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_maps); 11 12 tenki_text = (TextView) findViewById(R.id.tenki_text); 13 new weatherApi().execute(); 14 } 15 16 17 18 19public class weatherApi extends AsyncTask<String , String, String> { 20 21 22 @Override 23 protected String doInBackground(String... params) { 24 System.out.println("うううううううううう"); 25 26 //下記のURLデータを取得 27 String requestURL = "http://api.openweathermap.org/data/2.5/weather?id=1857910&appid=id"; 28 URL url = null; 29 try { 30 url = new URL(requestURL); 31 } catch (MalformedURLException e) { 32 e.printStackTrace(); 33 } 34 35 36 InputStream is = null; 37 try { 38 is = url.openConnection().getInputStream(); 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 43 // JSON形式で結果が返るためパースのためにStringに変換する 44 BufferedReader reader = null; 45 try { 46 reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 47 } catch (UnsupportedEncodingException e) { 48 e.printStackTrace(); 49 } 50 51 52 StringBuilder sb = new StringBuilder(); 53 String line; 54 try { 55 while (null != (line = reader.readLine())) { 56 sb.append(line); 57 } 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 String data11 = sb.toString(); 62 return sb.toString(); 63 64 65 66 } 67 68 @Override 69 protected void onPostExecute(String sb) { 70 try { 71 JSONArray jsonArray = new JSONArray(sb); 72 JSONObject jsonObject = jsonArray.getJSONObject(1); 73 String p = jsonObject.getString("main"); 74 75 76 tenki_text.setText(p); 77 78 } catch (JSONException e) { 79 e.printStackTrace(); 80 } 81 } 82 } 83 84 85 86 } 87
xml
1 2<TextView 3 android:id="@+id/tenki_text" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content" 6 android:layout_weight="1" 7 android:text="tenki_text" 8 android:textSize="24sp" />
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。