|

Dartの練習11-3:同期・非同期(APIでjsonデータを取得)

コアサーバ内に気象台のリストを作成し、phpでAPI経由で取得できるようにしました。


import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

void main(List arguments) async {
  var url = Uri.parse(
      'https://www.wsmeguro.jp/weather/weather_data.php?input_data=list');
  var response = await http.get(url);
  if (response.statusCode == 200) {
    //var jsonResponse = convert.jsonDecode(convert.utf8.decode(response.body.runes.toList()));
    var jsonResponse = convert.jsonDecode(response.body);
    var itemCount = jsonResponse.length;
    print('気象台の数: $itemCount.');
    for (var weatherData in jsonResponse) {
      print(weatherData['name'] + ' : ' + weatherData['code']);
    }
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }
}

類似投稿

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.