#androidStudioで天気予報のアプリを作りたいのですが、jsonで取得したデータが出力出来ません。
####[参照]天気予報のアプリ
###【activity_main.xml】
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/messageTextView" 11 android:text="Hello World!!" 12 android:textSize="32sp" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 app:layout_constraintTop_toTopOf="parent" 16 app:layout_constraintLeft_toLeftOf="parent" 17 android:layout_marginLeft="101dp" 18 android:layout_marginTop="81dp" /> 19 20 <Button 21 android:id="@+id/mybottun" 22 android:onClick="changeTextView" 23 android:text="ラベルを変更" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 app:layout_constraintTop_toTopOf="parent" 27 app:layout_constraintLeft_toLeftOf="parent" 28 android:layout_marginLeft="138dp" 29 android:layout_marginTop="160dp" /> 30 31</androidx.constraintlayout.widget.ConstraintLayout>
###【AndroidManifest.xml】
xml
1?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.ddd"> 4 <!-- ネット接続を許可する --> 5 <uses-permission android:name="android.permission.INTERNET" /> 6 <application 7 android:allowBackup="true" 8 android:icon="@mipmap/ic_launcher" 9 android:label="@string/app_name" 10 android:roundIcon="@mipmap/ic_launcher_round" 11 android:supportsRtl="true" 12 android:theme="@style/AppTheme"> 13 14 <activity android:name=".MainActivity"> 15 <intent-filter> 16 17 <action android:name="android.intent.action.MAIN" /> 18 19 <category android:name="android.intent.category.LAUNCHER" /> 20 21 </intent-filter> 22 </activity> 23 </application> 24 25</manifest>
###【MainActivity.java】
java
1package com.example.ddd; 2 3import androidx.appcompat.app.AppCompatActivity; 4import java.net.MalformedURLException; 5import java.net.URL; 6import android.os.Bundle; 7import android.view.View; 8import android.widget.TextView; 9 10public class MainActivity extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 } 17 18 public void changeTextView(View view) { 19 // 非同期処理(AsyncHttpRequest#doInBackground())を呼び出す 20 try { 21 new AsyncHttpRequest(this).execute(new URL("http://api.openweathermap.org/data/2.5/forecast?id=1850147&units=metric&cnt=1&appid={MY APEKEY}")); 22 } catch (MalformedURLException e) { 23 e.printStackTrace(); 24 } 25 } 26}
###【AsyncHttpRequest.java】
java
1package com.example.ddd; 2 3import android.app.Activity; 4import android.os.AsyncTask; 5import android.widget.TextView; 6import com.example.ddd.R; 7import org.json.JSONException; 8import org.json.JSONObject; 9import java.io.BufferedReader; 10import java.io.IOException; 11import java.io.InputStream; 12import java.io.InputStreamReader; 13import java.net.HttpURLConnection; 14import java.net.URL; 15/** 16 * 非同期処理を行うクラス. 17 */ 18public final class AsyncHttpRequest extends AsyncTask<URL, Void, String> { 19 private int TODAY_FORCAST_INDEX = 0; 20 private Activity mainActivity; 21 22 public AsyncHttpRequest(Activity activity) { 23 // 呼び出し元のアクティビティ 24 this.mainActivity = activity; 25 } 26 27 /** 28 * 非同期処理で天気情報を取得する. 29 * @param urls 接続先のURL 30 * @return 取得した天気情報 31 */ 32 @Override 33 protected String doInBackground(URL... urls) { 34 35 final URL url = urls[0]; 36 HttpURLConnection con = null; 37 38 try { 39 con = (HttpURLConnection) url.openConnection(); 40 con.setRequestMethod("GET"); 41 // リダイレクトを自動で許可しない設定 42 con.setInstanceFollowRedirects(false); 43 con.connect(); 44 45 final int statusCode = con.getResponseCode(); 46 if (statusCode != HttpURLConnection.HTTP_OK) { 47 System.err.println("正常に接続できていません。statusCode:" + statusCode); 48 return null; 49 } 50 51 // レスポンス(JSON文字列)を読み込む準備 52 final InputStream in = con.getInputStream(); 53 String encoding = con.getContentEncoding(); 54 if(null == encoding){ 55 encoding = "UTF-8"; 56 } 57 final InputStreamReader inReader = new InputStreamReader(in, encoding); 58 final BufferedReader bufReader = new BufferedReader(inReader); 59 StringBuilder response = new StringBuilder(); 60 String line = null; 61 // 1行ずつ読み込む 62 while((line = bufReader.readLine()) != null) { 63 response.append(line); 64 } 65 bufReader.close(); 66 inReader.close(); 67 in.close(); 68 69 // 受け取ったJSON文字列をパース 70 JSONObject jsonObject = new JSONObject(response.toString()); 71 JSONObject todayForcasts = jsonObject.getJSONArray("weather").getJSONObject(TODAY_FORCAST_INDEX); 72 73 return "今日の天気は" + todayForcasts.getString("main"); 74 } catch (IOException e) { 75 e.printStackTrace(); 76 return null; 77 } catch (JSONException e) { 78 e.printStackTrace(); 79 return null; 80 } finally { 81 if (con != null) { 82 con.disconnect(); 83 } 84 } 85 } 86 87 /** 88 * 非同期処理が終わった後の処理. 89 * @param result 非同期処理の結果得られる文字列 90 */ 91 @Override 92 protected void onPostExecute(String result) { 93 TextView tv = mainActivity.findViewById(R.id.messageTextView); 94 tv.setText(result); 95 } 96} 97
###【ターミナル】
iMac215-2011:ddd admin$ curl "http://api.openweathermap.org/data/2.5/forecast?id=1850147&units=metric&cnt=1&appid="MyAppID" | jq '.' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 590 100 590 0 0 2163 0 --:--:-- --:--:-- --:--:-- 2169 { "cod": "200", "message": 0, "cnt": 1, "list": [ { "dt": 1607860800, "main": { "temp": 10.04, "feels_like": 6.37, "temp_min": 10.04, "temp_max": 10.54, "pressure": 1010, "sea_level": 1010, "grnd_level": 1006, "humidity": 78, "temp_kf": -0.5 }, "weather": [ { "id": 802, "main": "Clouds", "description": "scattered clouds", "icon": "03n" } ], "clouds": { "all": 43 }, "wind": { "speed": 4.05, "deg": 97 }, "visibility": 10000, "pop": 0, "sys": { "pod": "n" }, "dt_txt": "2020-12-13 12:00:00" } ], "city": { "id": 1850147, "name": "Tokyo", "coord": { "lat": 35.6895, "lon": 139.6917 }, "country": "JP", "population": 0, "timezone": 32400, "sunrise": 1607809343, "sunset": 1607844516 } } iMac215-2011:ddd admin$
回答1件
あなたの回答
tips
プレビュー