关于flutter:Flutter-Dio源码分析二HttpClientHttpDio对比

50次阅读

共计 3361 个字符,预计需要花费 9 分钟才能阅读完成。

文章系列

Flutter Dio 源码剖析(一)–Dio 介绍

Flutter Dio 源码剖析(二)–HttpClient、Http、Dio 比照

Flutter Dio 源码剖析(三)– 深度分析

Flutter Dio 源码剖析(四)– 封装

视频系列

Flutter Dio 源码剖析(一)–Dio 介绍视频教程

Flutter Dio 源码剖析(二)–HttpClient、Http、Dio 比照视频教程

Flutter Dio 源码剖析(三)– 深度分析视频教程

Flutter Dio 源码剖析(四)– 封装视频教程

源码仓库地址

github 仓库地址

前言

在前文中咱们对 Dio 进行了根本介绍,也写了一个简略的示例,明天咱们持续来讲一下 Flutter 网络申请的三种申请形式的比照,以达到更好了解Dio 网络申请库的目标。

零碎自带网络申请 HttpClient

步骤一:创立一个 HttpClient

HttpClient httpClient = HttpClient();

步骤二:关上 http 连贯, 设置申请头

HttpClientRequest request = await httpClient.getUrl(Uri.parse("http://localhost:8080/getUserInfo"));

步骤三:通过 HttpClientRequest 能够设置申请 header

request.headers.add("token", "123456");

步骤四:期待连贯服务器

HttpClientResponse response = await request.close();

步骤五:读取响应内容

// 响应流数据以 utf8 编码格局返回
String responseBody = await response.transform(utf8.decoder).join();

步骤六:申请完结, 敞开 httpClient

httpClient.close();

残缺示例代码

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

class HttpClientExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {void _getUserInfo() async {
      try {
        // 1. 创立 httpClient
        HttpClient httpClient = HttpClient();
        // 2. 关上 http 连贯, 设置申请头
        HttpClientRequest request = await httpClient.getUrl(Uri.parse("http://localhost:8080/getUserInfo"));
        // 3. 通过 HttpClientRequest 能够设置申请 header
        request.headers.add("token", "123456");
        // 4. 期待连贯服务器
        HttpClientResponse response = await request.close();
        // 5. 读取响应内容
        String responseBody = await response.transform(utf8.decoder).join();
        // 6. 申请完结, 敞开 httpClient
        httpClient.close();

        print(responseBody);
      } catch (e) {print(e);
      }
    }

    return Scaffold(
      appBar: AppBar(title: Text("DioExample"),
      ),
      body: Center(
        child: Column(
          children: [
            TextButton(
              onPressed: _getUserInfo,
              child: Text("发送 get 申请"),
            )
          ],
        ),
      ),
    );
  }
}

第三方网络申请库 Http

步骤一:增加依赖

dependencies:
  http: ^0.13.3 #latest version

步骤二:导入库

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

步骤三:发送申请

var response = await http.post(Uri.parse("http://localhost:8080/getUserInfo"));

残缺实例代码

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

class HttpClientExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {void _getUserInfo() async {
      try {
        // 1. 创立 httpClient
        HttpClient httpClient = HttpClient();
        // 2. 关上 http 连贯, 设置申请头
        HttpClientRequest request = await httpClient.getUrl(Uri.parse("http://localhost:8080/getUserInfo"));
        // 3. 通过 HttpClientRequest 能够设置申请 header
        request.headers.add("token", "123456");
        // 4. 期待连贯服务器
        HttpClientResponse response = await request.close();
        // 5. 读取响应内容
        String responseBody = await response.transform(utf8.decoder).join();
        // 6. 申请完结, 敞开 httpClient
        httpClient.close();

        print(responseBody);
      } catch (e) {print(e);
      }
    }

    return Scaffold(
      appBar: AppBar(title: Text("DioExample"),
      ),
      body: Center(
        child: Column(
          children: [
            TextButton(
              onPressed: _getUserInfo,
              child: Text("发送 get 申请"),
            )
          ],
        ),
      ),
    );
  }
}

第三方网络申请库 Dio

步骤一:增加依赖

dependencies:
  dio: ^4.0.0 #latest version

步骤二:导入库

import 'package:dio/dio.dart';

步骤三:发送申请

var response = await Dio().get('http://localhost:8080/getUserInfo');

残缺示例代码

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class DioExample extends StatelessWidget {void _getUserInfo() async {
    try {var response = await Dio().get('http://localhost:8080/getUserInfo');
      print(response);
    } catch (e) {print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("DioExample"),
      ),
      body: Center(
        child: Column(
          children: [
            TextButton(
              onPressed: _getUserInfo,
              child: Text("发送 get 申请"),
            )
          ],
        ),
      ),
    );
  }
}

总结

原生 HttpClient 发动网络申请十分的简单,很多货色还需本人手动解决。如果波及到 上传 下载 断点续传 等那必定十分繁琐,不倡议应用。再来说一下Diohttp 两个第三方组件,他们封装的性能都差不多,反而 Dio 更弱小易用,而且从gitbub 的 Star 来说,Dio10000 star,而 http 才 691 star,该数据由 2021 年 08 月 24 日统计。

正文完
 0