:::

5-2 HTTP Client 請求

  1. 須導入convertio
  2. 完整範例:
    import 'package:flutter/material.dart';
    import 'dart:convert';
    import 'dart:io';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      void getWeatherData() async {
        try {
          HttpClient httpClient = new HttpClient();
          HttpClientRequest request = await httpClient.getUrl(Uri.parse("http://t.weather.sojson.com/api/weather/city/101030100"));
          HttpClientResponse response=await request.close();
          var result=await response.transform(utf8.decoder).join();
          print(result);
          httpClient.close();
        } catch (e) {
          print('請求失敗!');
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'httpClient 請求範例',
          home: new Scaffold(
            appBar: new AppBar(
              title: new Text('httpClient 請求範例'),
            ),
            body: new Center(
              child: new RaisedButton(
                child: new Text('取得天氣數據'),
                onPressed: getWeatherData,
              ),
            ),
          ),
        );
      }
    }
    

    執行結果可以從編輯器下方的「偵錯主控台」觀看(並不會呈現在主機上)


:::

書籍目錄

展開 | 闔起

https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1356

計數器

今天: 2872287228722872
昨天: 1988198819881988
總計: 7389763738976373897637389763738976373897637389763