Flutter 網絡請求框架封裝

Flutter 請求網絡的三種方式

flutter 請求網絡的方式有三種,分別是 Dart 原生的網絡請求 HttpClient、第三方網絡請求 http以及 Flutter 中的 Dio。咱們能夠比較一下這三種網絡請求方式,而後封裝爲咱們方便請求網絡的工具類。git

Dart 原生的網絡請求 HttpClient

實現 Dart 獲取網絡數據的請求,通常咱們須要如下幾個步驟:github

  • step 1: 原生的網絡請求時不須要修改 pubspec.yaml 文件的,咱們只須要在使用的地方引入所需包就能夠了
import 'dart:convert';
import 'dart:io';
複製代碼
  • step 2:建立一個HttpClient
HttpClient httpClient = new HttpClient();
複製代碼
  • step 3: 打開Http鏈接,設置請求頭
HttpClientRequest request = await httpClient.getUrl(uri);
複製代碼

在這一步中,咱們能夠設置人意的的請求方法,好比 Get 請求、Post 請求、Delete 請求。json

例如:攜帶參數的請求bash

Uri uri=Uri(scheme: "https", host: "flutterchina.club", queryParameters: {
    "userName":"chen",
    "password":"123456"
  });
複製代碼

例如:設置請求的 header服務器

request.headers.add("user-agent", "test");
request.headers.add("Authorization", "LKSJDLFJSDLKJSLKklsdj");
複製代碼
  • step 4: 等待鏈接服務器
HttpClientResponse response = await request.close();
複製代碼
  • step 5: 讀取響應內容
if (response.statusCode == HttpStatus.ok) {
      _content = await response.transform(Utf8Decoder()).join();
}
複製代碼
  • step 6: 斷開鏈接
httpClient.close();
複製代碼

以上的步驟是 dart 簡單獲取網絡的方式,咱們從上面能夠看到,經過 HttpClient 發起網絡請求時比較麻煩的,不少都要咱們親手處理,還有 Cookie 的管理也是比較麻煩的。網絡

庫 http

  • step 1:pubspec.yaml 添加依賴
http: '>=0.11.3+12'
複製代碼
  • step 2: 在使用的地方導包
import 'package:http/http.dart' as http;
複製代碼
  • step 3: 發起請求


Get 請求app

void getRequest() async {
    var client = http.Client();
    http.Response response = await client.get(url_2);
    _content = response.body;
  }
複製代碼


Post 請求框架

void postRequest() async {
    var params = Map<String, String>();
    params["username"] = "hellonews";
    params["password"] = "123456";

    var client = http.Client();
    var response = await client.post(url_post, body: params);
    _content = response.body;
  }
複製代碼

相對比 Dart 原生的網絡請求,第三方庫 http 的網絡請求方式是要方便好多,寫起來也是挺爽的。async

Flutter 發佈的 dio

Dio 一個強大的 Dart Http 請求庫,支持 Restful API、FormData、攔截器、請求取消、Cookie管理、文件上傳/下載、超時等...工具

  • step 1:pubspec.yaml 添加依賴
dependencies:
  dio: ^1.0.9
複製代碼
  • step 2:導入引用包
import 'package:dio/dio.dart';
複製代碼
  • step 3:發起網絡請求


Get 請求

void getRequest() async {
    Dio dio = new Dio();
    var response = await dio.get("/test?id=12&name=chen");
    _content = response.data.toString();
  }
複製代碼

對於 query 參數,咱們能夠經過對象來進行傳遞,上面的代碼等同於:

void getRequest() async {
    Dio dio = new Dio();
    var response = await dio.get("/test",data:{"id":12,"name":"chen"});
    _content = response.data.toString();
  }
複製代碼

Post 請求

void postRequest() async {
    var dio = new Dio();
    var response = await dio.post(url_post, data:{"id":12,"name":"wendu"});
    _content = response.data.toString();
  }
複製代碼

Dio 網絡請求框架封裝

日誌信息攔截

Dio 和 okhttp 同樣,都會有一個請求攔截器和響應攔截器,經過攔截器,咱們能夠在請求以前或響應以後作一些贊成的預處理。例如咱們發起請求前查看咱們請求的參數和頭部,響應的時候,咱們能夠查看返回來的數據。

Dio dio = new Dio();
    // 添加攔截器
    if (Config.DEBUG) {
      dio.interceptors.add(InterceptorsWrapper(
          onRequest: (RequestOptions options){
            print("\n================== 請求數據 ==========================");
            print("url = ${options.uri.toString()}");
            print("headers = ${options.headers}");
            print("params = ${options.data}");
          },
          onResponse: (Response response){
            print("\n================== 響應數據 ==========================");
            print("code = ${response.statusCode}");
            print("data = ${response.data}");
            print("\n");
          },
          onError: (DioError e){
            print("\n================== 錯誤響應數據 ======================");
            print("type = ${e.type}");
            print("message = ${e.message}");
            print("stackTrace = ${e.stackTrace}");
            print("\n");
          }
      ));
    }
複製代碼

若是咱們想要移除攔截器,那麼咱們能夠將其設置爲 null

dio.interceptor.request.onSend=null;
dio.interceptor.response.onSuccess=null;
dio.interceptor.response.onError=null;
複製代碼

token 添加

//  頭部添加 token 驗證
    headers["Authorization"] = "token lskjdlklsjkdklsjd333";
    option.headers = headers;
    ///超時
    option.connectTimeout = 15000;
    try {
      Response response = await dio.request(url, data: params, options: option);
    } on DioError catch (e) {
      // 請求錯誤處理
    }
複製代碼

自動生成 dart 的 json 實體類插件 FlutterJsonBeanFactory

在 Android 開發中,有 GsonFormat 這個插件來說 json 數據自動轉化成 Bean;那麼在 Flutter 中也有相似的插件能夠生產序列化的實體類的插件:FlutterJsonBeanFactory

  • step 1:下載插件 FlutterJsonBeanFactory,安裝完成後重啓
Setting -> Plugins -> Browse Respositories 中搜索 FlutterJsonBeanFactory
複製代碼
  • step 2:建立實體類,在指定目錄下:
New -> dart bean class File from JSON
複製代碼

jsontobean.png

  • step 3:輸入實體類名及 json 格式的數據

    format.png

  • step 4:最後生成的實體類:LoginEntity

class LoginEntity {
	String easemobpassword;
	String username;

	LoginEntity({this.easemobpassword, this.username});

	LoginEntity.fromJson(Map<String, dynamic> json) {
		easemobpassword = json['easemobPassword'];
		username = json['username'];
	}

	Map<String, dynamic> toJson() {
		final Map<String, dynamic> data = new Map<String, dynamic>();
		data['easemobPassword'] = this.easemobpassword;
		data['username'] = this.username;
		return data;
	}
}
複製代碼

請求錯誤處理

Response response;
    try {
      response = await dio.request(url, data: params, options: option);
    } on DioError catch (e) {
      // 請求錯誤處理
      Response errorResponse;
      if (e.response != null) {
        errorResponse = e.response;
      } else {
        errorResponse = new Response(statusCode: 666);
      }
      if (e.type == DioErrorType.CONNECT_TIMEOUT) {
        errorResponse.statusCode = Code.NETWORK_TIMEOUT;
      }
      if (Config.DEBUG) {
        print('請求異常: ' + e.toString());
        print('請求異常 url: ' + url);
      }
      return new ResultData(Code.errorHandleFunction(errorResponse.statusCode, e.message, noTip), false, errorResponse.statusCode);
    }
複製代碼

其中 ResultData 是網絡結果處理的實體類

/**
 * 網絡結果數據
 * Created by chenjianrun
 * Date: 2018-07-16
 */
class ResultData {
  var data;
  bool result;
  int code;
  var headers;

  ResultData(this.data, this.result, this.code, {this.headers});
}

複製代碼

Code 是處理網絡錯誤的編碼,並將錯誤結果經過 eventbus 發送出去,通常咱們能夠在 main_pager 中註冊監聽這個事件。

///網絡請求錯誤編碼
class Code {
  ///網絡錯誤
  static const NETWORK_ERROR = -1;

  ///網絡超時
  static const NETWORK_TIMEOUT = -2;

  ///網絡返回數據格式化一次
  static const NETWORK_JSON_EXCEPTION = -3;

  static const SUCCESS = 200;

  static final EventBus eventBus = new EventBus();

  static errorHandleFunction(code, message, noTip) {
    if(noTip) {
      return message;
    }
    eventBus.fire(new HttpErrorEvent(code, message));
    return message;
  }
}
複製代碼

完成的網絡請求類:HttpRequest

import 'dart:io';

import 'package:dio/dio.dart';
import 'package:private_tutor/common/SpUtils.dart';
import 'package:connectivity/connectivity.dart';

import 'dart:collection';

import 'package:private_tutor/common/config/Config.dart';
import 'package:private_tutor/net/ResultCode.dart';
import 'package:private_tutor/net/ResultData.dart';

///http請求管理類,可單獨抽取出來
class HttpRequest {
  static String _baseUrl;
  static const CONTENT_TYPE_JSON = "application/json";
  static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
  static Map optionParams = {
    "timeoutMs": 15000,
    "token": null,
    "authorizationCode": null,
  };

  static setBaseUrl(String baseUrl){
    _baseUrl = baseUrl;
  }

  static get(url,param) async{
    return await request(_baseUrl+url, param, null, new Options(method:"GET"));
  }

  static post(url,param) async{
    return await request(_baseUrl+url, param, {"Accept": 'application/vnd.github.VERSION.full+json'}, new Options(method: 'POST'));
  }

  static delete(url,param) async{
    return await request(_baseUrl+url, param, null, new Options(method: 'DELETE'));
  }

  static put(url,param) async{
    return await request(_baseUrl+url, param, null, new Options(method: "PUT", contentType: ContentType.text));
  }

  ///發起網絡請求
  ///[ url] 請求url
  ///[ params] 請求參數
  ///[ header] 外加頭
  ///[ option] 配置
  static request(url, params, Map<String, String> header, Options option, {noTip = false}) async {

    //沒有網絡
    var connectivityResult = await (new Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.none) {
      return new ResultData(Code.errorHandleFunction(Code.NETWORK_ERROR, "", noTip), false, Code.NETWORK_ERROR);
    }

    Map<String, String> headers = new HashMap();
    if (header != null) {
      headers.addAll(header);
    }

    //受權碼
    if (optionParams["authorizationCode"] == null) {
      var authorizationCode = await getAuthorization();
      if (authorizationCode != null) {
        optionParams["authorizationCode"] = authorizationCode;
      }
    }

    headers["Authorization"] = optionParams["authorizationCode"];
    // 設置 baseUrl
    
    if (option != null) {
      option.headers = headers;
    } else{
      option = new Options(method: "get");
      option.headers = headers;
    }

    ///超時
    option.connectTimeout = 15000;

    Dio dio = new Dio();
    // 添加攔截器
    if (Config.DEBUG) {
      dio.interceptors.add(InterceptorsWrapper(
          onRequest: (RequestOptions options){
            print("\n================== 請求數據 ==========================");
            print("url = ${options.uri.toString()}");
            print("headers = ${options.headers}");
            print("params = ${options.data}");
          },
          onResponse: (Response response){
            print("\n================== 響應數據 ==========================");
            print("code = ${response.statusCode}");
            print("data = ${response.data}");
            print("\n");
          },
          onError: (DioError e){
            print("\n================== 錯誤響應數據 ======================");
            print("type = ${e.type}");
            print("message = ${e.message}");
            print("stackTrace = ${e.stackTrace}");
            print("\n");
          }
      ));
    }

    Response response;
    try {
      response = await dio.request(url, data: params, options: option);
    } on DioError catch (e) {
      // 請求錯誤處理
      Response errorResponse;
      if (e.response != null) {
        errorResponse = e.response;
      } else {
        errorResponse = new Response(statusCode: 666);
      }
      if (e.type == DioErrorType.CONNECT_TIMEOUT) {
        errorResponse.statusCode = Code.NETWORK_TIMEOUT;
      }
      if (Config.DEBUG) {
        print('請求異常: ' + e.toString());
        print('請求異常 url: ' + url);
      }
      return new ResultData(Code.errorHandleFunction(errorResponse.statusCode, e.message, noTip), false, errorResponse.statusCode);
    }

    try {
      if (option.contentType != null && option.contentType.primaryType == "text") {
        return new ResultData(response.data, true, Code.SUCCESS);
      } else {
        var responseJson = response.data;
        if (response.statusCode == 201 && responseJson["token"] != null) {
          optionParams["authorizationCode"] = 'token ' + responseJson["token"];
          await SpUtils.save(Config.TOKEN_KEY, optionParams["authorizationCode"]);
        }
      }
      if (response.statusCode == 200 || response.statusCode == 201) {
        return ResultData(response.data, true, Code.SUCCESS, headers: response.headers);
      }
    } catch (e) {
      print(e.toString() + url);
      return ResultData(response.data, false, response.statusCode, headers: response.headers);
    }
    return new ResultData(Code.errorHandleFunction(response.statusCode, "", noTip), false, response.statusCode);
  }

  ///清除受權
  static clearAuthorization() {
    optionParams["authorizationCode"] = null;
    SpUtils.remove(Config.TOKEN_KEY);
  }

  ///獲取受權token
  static getAuthorization() async {
    String token = await SpUtils.get(Config.TOKEN_KEY);
    if (token == null) {
      String basic = await SpUtils.get(Config.USER_BASIC_CODE);
      if (basic == null) {
        //提示輸入帳號密碼
      } else {
        //經過 basic 去獲取token,獲取到設置,返回token
        return "Basic $basic";
      }
    } else {
      optionParams["authorizationCode"] = token;
      return token;
    }
  }
}

複製代碼

使用示例

/// 登陸 model
class LoginModel{
    // 手機號碼登陸
  static phoneLogin(String phone,String verifyCode) async{
    ResultData response = await HttpRequest.post(Address.phoneLogin, {"phoneNum" : phone,"captcha":verifyCode});
    if(response != null && response.result){
        PhoneLoginEntity phoneLoginEntity = PhoneLoginEntity.fromJson(json.decode(response.data));
        return new DataResult(phoneLoginEntity, true);
    }else{
      return new DataResult(null, false);
    }
  }

    // 獲取驗證碼
  static getVerifyCode(String phone) async{
    ResultData response = await HttpRequest.get("${Address.getVerifyCode}?phone=${phone}", null);

//    var response = await HttpRequest.get(Address.getVerifyCode, {"phone":phone});
    if(response != null && response.result){
      VerifyCodeEntity entity = VerifyCodeEntity.fromJson(response.data);
      return new DataResult(entity, true);
    }else{
      return new DataResult(null, false);
    }
  }
}
複製代碼

做者介紹

  • 陳堅潤:廣州蘆葦科技 APP 團隊 Android 開發工程師

內推信息

  • 咱們正在招募小夥伴,有興趣的小夥伴能夠把簡歷發到 app@talkmoney.cn,備註:來自掘金社區
  • 詳情能夠戳這裏--> 廣州蘆葦信息科技
相關文章
相關標籤/搜索