日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Flutter教程:Flutter網(wǎng)絡(luò)和Http

發(fā)起HTTP請(qǐng)求

http支持位于dart:io,所以要?jiǎng)?chuàng)建一個(gè)HTTP client, 我們需要添加一個(gè)導(dǎo)入:

為渭源等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及渭源網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、渭源網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

import 'dart:io';

var httpClient = new HttpClient();

該 client 支持常用的HTTP操作, such as GET, POST, PUT, DELETE.

處理異步

注意,HTTP API 在返回值中使用了Dart Futures。 我們建議使用async/await語法來調(diào)用API。

網(wǎng)絡(luò)調(diào)用通常遵循如下步驟:

  1. 創(chuàng)建 client.
  2. 構(gòu)造 Uri.
  3. 發(fā)起請(qǐng)求, 等待請(qǐng)求,同時(shí)您也可以配置請(qǐng)求headers、 body。
  4. 關(guān)閉請(qǐng)求, 等待響應(yīng).
  5. 解碼響應(yīng)的內(nèi)容.

Several of these steps use Future based APIs. Sample APIs calls for each step above are: 其中的幾個(gè)步驟使用基于Future的API。上面步驟的示例:

get() async {
  var httpClient = new HttpClient();
  var uri = new Uri.http(
      'example.com', '/path1/path2', {'param1': '42', 'param2': 'foo'});
  var request = await httpClient.getUrl(uri);
  var response = await request.close();
  var responseBody = await response.transform(UTF8.decoder).join();
}

有關(guān)完整的代碼示例,請(qǐng)參閱下面的“示例”。

解碼和編碼JSON

使用dart:convert庫可以簡(jiǎn)單解碼和編碼JSON。 有關(guān)其他的JSON文檔,請(qǐng)參閱JSON和序列化。

解碼簡(jiǎn)單的JSON字符串并將響應(yīng)解析為Map:

Map data = JSON.decode(responseBody);
// Assume the response body is something like: ['foo', { 'bar': 499 }]
int barValue = data[1]['bar']; // barValue is set to 499

要對(duì)簡(jiǎn)單的JSON進(jìn)行編碼,請(qǐng)將簡(jiǎn)單值(字符串,布爾值或數(shù)字字面量)或包含簡(jiǎn)單值的Map,list等傳給encode方法:

String encodedString = JSON.encode([1, 2, { 'a': null }]);

示例: 解碼 HTTPS GET請(qǐng)求的JSON

以下示例顯示了如何在Flutter應(yīng)用中對(duì)HTTPS GET請(qǐng)求返回的JSON數(shù)據(jù)進(jìn)行解碼

It calls the httpbin.com web service testing API, which then responds with your local IP address. Note that secure networking (HTTPS) is used. 它調(diào)用httpbin.comWeb service測(cè)試API,請(qǐng)注意,使用安全網(wǎng)絡(luò)請(qǐng)求(HTTPS)

  1. 運(yùn)行flutter create,創(chuàng)建一個(gè)新的Flutter應(yīng)用.
  2. 將lib/main.dart替換為一下內(nèi)容:
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State {
  var _ipAddress = 'Unknown';

  _getIPAddress() async {
    var url = 'https://httpbin.org/ip';
    var httpClient = new HttpClient();

    String result;
    try {
      var request = await httpClient.getUrl(Uri.parse(url));
      var response = await request.close();
      if (response.statusCode == HttpStatus.OK) {
        var json = await response.transform(utf8.decoder).join();
        var data = jsonDecode(json);
        result = data['origin'];
      } else {
        result =
            'Error getting IP address:\nHttp status ${response.statusCode}';
      }
    } catch (exception) {
      result = 'Failed getting IP address';
    }

    // If the widget was removed from the tree while the message was in flight,
    // we want to discard the reply rather than calling setState to update our
    // non-existent appearance.
    if (!mounted) return;

    setState(() {
      _ipAddress = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    var spacer = new SizedBox(height: 32.0);

    return new Scaffold(
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            new Text('Your current IP address is:'),
            new Text('$_ipAddress.'),
            spacer,
            new RaisedButton(
              onPressed: _getIPAddress,
              child: new Text('Get IP address'),
            ),
          ],
        ),
      ),
    );
  }
}

API 文檔

有關(guān)完整的API文檔,請(qǐng)參閱:

  • 庫dart:io
  • 庫dart:convert

文章標(biāo)題:創(chuàng)新互聯(lián)Flutter教程:Flutter網(wǎng)絡(luò)和Http
網(wǎng)站地址:http://www.5511xx.com/article/ccohjhi.html