前提・実現したいこと
「android アプリ開発の教科書 基礎&応用をしっかり育成!」
という書籍でjavaでandroidアプリ開発の勉強をしています。
11章 非同期処理とweb api連携のところで質問です。
ライブドアの天気情報から現在の情報を取得し、アプリ上でクリックした市町村の情報を表示したいです。
発生している問題・エラーメッセージ
特段エラーは出ていないのですが、クリックしても情報を表示できません。
該当のソースコード
WeatherInfoActivity.java
public class WeatherInfoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_weather_info); //画面部品ListViewを取得 ListView lvCityList = findViewById(R.id.lvCityList); List<Map<String,String>> cityList = new ArrayList<>(); Map<String ,String> city = new HashMap<>(); city.put("name","大阪"); city.put("id","270000"); cityList.add(city); city = new HashMap<>(); city.put("name","神戸"); city.put("id","280010"); cityList.add(city); city = new HashMap<>(); city.put("name","豊岡"); city.put("id","652872"); cityList.add(city); city = new HashMap<>(); city.put("name","京都"); city.put("id","572849"); cityList.add(city); city = new HashMap<>(); city.put("name","舞鶴"); city.put("id","285698"); cityList.add(city); // SimpleAdapterで使用するfrom-to変数の用意 String [] from = {"name"}; int[] to ={android.R.id.text1}; SimpleAdapter adapter = new SimpleAdapter(WeatherInfoActivity.this,cityList,android.R.layout.simple_expandable_list_item_1,from,to); lvCityList.setAdapter(adapter); lvCityList.setOnItemClickListener(new ListItemClickListener()); } private class ListItemClickListener implements AdapterView.OnItemClickListener{ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String,String> item = (Map<String, String>) parent.getItemAtPosition(position); String cityName = item.get("name"); String cityId = item.get("id"); TextView tvCityName = findViewById(R.id.tvCityName); tvCityName.setText(cityName + "の天気:"); //WeatherInfoReceiverを実行する処理 TextView tvWeatherTelop = findViewById(R.id.tvWeatherTelop); TextView tvWeatherDesc = findViewById(R.id.tvWeatherDesc); WeatherInfoReceiver receiver = new WeatherInfoReceiver(tvWeatherTelop,tvWeatherDesc); receiver.execute(cityId); } } //非同期処理のコード private class WeatherInfoReceiver extends AsyncTask<String,String,String> { private TextView _tvWeatherTelop; private TextView _tvWeatherDesc; public WeatherInfoReceiver(TextView tvWeatherTelop,TextView tvWeatherDesc){ _tvWeatherTelop = tvWeatherTelop; _tvWeatherDesc = tvWeatherDesc; } @Override public String doInBackground(String...params){ String id = params[0]; String urlStr ="http://weather.livedoor.com/forecast/webservice/json/v1?city=" + id; String result = ""; // インターネットに接続する処理 HttpURLConnection con = null; InputStream is = null; try { URL url = new URL(urlStr); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.connect(); is = con.getInputStream(); result = is2String(is); } catch(MalformedURLException ex){ } catch(IOException ex){ } finally { if(con != null){ con.disconnect(); } if(is != null){ try{ is.close(); } catch(IOException ex){ } } } return result; } @Override public void onPostExecute(String result){ String telop = ""; String desc = ""; //json文字列を解析する処理 try { JSONObject rootJSON = new JSONObject(result); JSONObject descriptionJSON = rootJSON.getJSONObject("description"); desc = descriptionJSON.getString("text"); JSONArray forecasts = rootJSON.getJSONArray("forecasts"); JSONObject forecastNow = forecasts.getJSONObject(0); telop = forecastNow.getString("telop"); } catch (JSONException ex){ } _tvWeatherTelop.setText(telop); _tvWeatherDesc.setText(desc); } //inputStreamオブジェクトを文字列に変換するメソッド private String is2String(InputStream is) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); StringBuffer sb = new StringBuffer(); char[] b = new char[1024]; int line; while(0 <=(line = reader.read(b))){ sb.append(b,0,line); } return sb.toString(); } } }
試したこと
リンク先を確認したところ、ページが削除されていたので、そのせいかなと思っています。
しかし、どのURLに接続すべきなのかがわかりません。
ご教授頂きたいです。
よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
android studio/java/API15
回答1件
あなたの回答
tips
プレビュー