:::

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,
              ),
            ),
          ),
        );
      }
    }
    

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


:::

書籍目錄

展開 | 闔起

快速登入


http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1356%26tbsn%3D42

計數器

今天: 796796796
昨天: 3068306830683068
總計: 8077301807730180773018077301807730180773018077301