您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)使用Flutter怎么封裝一個(gè)網(wǎng)絡(luò)請(qǐng)求框架,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
Flutter 請(qǐng)求網(wǎng)絡(luò)的三種方式
flutter 請(qǐng)求網(wǎng)絡(luò)的方式有三種,分別是 Dart 原生的網(wǎng)絡(luò)請(qǐng)求 HttpClient、第三方網(wǎng)絡(luò)請(qǐng)求 http以及 Flutter 中的 Dio。我們可以比較一下這三種網(wǎng)絡(luò)請(qǐng)求方式,然后封裝為我們方便請(qǐng)求網(wǎng)絡(luò)的工具類。
Dart 原生的網(wǎng)絡(luò)請(qǐng)求 HttpClient
實(shí)現(xiàn) Dart 獲取網(wǎng)絡(luò)數(shù)據(jù)的請(qǐng)求,一般我們需要以下幾個(gè)步驟:
step 1: 原生的網(wǎng)絡(luò)請(qǐng)求時(shí)不需要修改 pubspec.yaml 文件的,我們只需要在使用的地方引入所需包就可以了
import 'dart:convert'; import 'dart:io';
step 2:創(chuàng)建一個(gè)HttpClient
HttpClient httpClient = new HttpClient();
step 3: 打開Http連接,設(shè)置請(qǐng)求頭
HttpClientRequest request = await httpClient.getUrl(uri);
在這一步中,我們可以設(shè)置人意的的請(qǐng)求方法,比如 Get 請(qǐng)求、Post 請(qǐng)求、Delete 請(qǐng)求。
例如:攜帶參數(shù)的請(qǐng)求
Uri uri=Uri(scheme: "https", host: "flutterchina.club", queryParameters: { "userName":"chen", "password":"123456" });
例如:設(shè)置請(qǐng)求的 header
request.headers.add("user-agent", "test"); request.headers.add("Authorization", "LKSJDLFJSDLKJSLKklsdj");
step 4: 等待連接服務(wù)器
HttpClientResponse response = await request.close();
step 5: 讀取響應(yīng)內(nèi)容
if (response.statusCode == HttpStatus.ok) { _content = await response.transform(Utf8Decoder()).join(); }
step 6: 斷開連接
httpClient.close();
以上的步驟是 dart 簡(jiǎn)單獲取網(wǎng)絡(luò)的方式,我們從上面可以看到,通過 HttpClient 發(fā)起網(wǎng)絡(luò)請(qǐng)求時(shí)比較麻煩的,很多都要我們親手處理,還有 Cookie 的管理也是比較麻煩的。
庫(kù) http step
1:pubspec.yaml 添加依賴
http: '>=0.11.3+12'
step 2: 在使用的地方導(dǎo)包
import 'package:http/http.dart' as http;
step 3: 發(fā)起請(qǐng)求
Get 請(qǐng)求
void getRequest() async { var client = http.Client(); http.Response response = await client.get(url_2); _content = response.body; }
Post 請(qǐng)求
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; }
相對(duì)比 Dart 原生的網(wǎng)絡(luò)請(qǐng)求,第三方庫(kù) http 的網(wǎng)絡(luò)請(qǐng)求方式是要方便好多,寫起來也是挺爽的。
Flutter 發(fā)布的 dio
Dio 一個(gè)強(qiáng)大的 Dart Http 請(qǐng)求庫(kù),支持 Restful API、FormData、攔截器、請(qǐng)求取消、Cookie管理、文件上傳/下載、超時(shí)等...
step 1:pubspec.yaml 添加依賴
dependencies: dio: ^1.0.9
step 2:導(dǎo)入引用包
import 'package:dio/dio.dart';
step 3:發(fā)起網(wǎng)絡(luò)請(qǐng)求
Get 請(qǐng)求
void getRequest() async { Dio dio = new Dio(); var response = await dio.get("/test?id=12&name=chen"); _content = response.data.toString(); }
對(duì)于 query 參數(shù),我們可以通過對(duì)象來進(jìn)行傳遞,上面的代碼等同于:
void getRequest() async { Dio dio = new Dio(); var response = await dio.get("/test",data:{"id":12,"name":"chen"}); _content = response.data.toString(); }
Post 請(qǐng)求
void postRequest() async { var dio = new Dio(); var response = await dio.post(url_post, data:{"id":12,"name":"wendu"}); _content = response.data.toString(); }
Dio 網(wǎng)絡(luò)請(qǐng)求框架封裝
日志信息攔截
Dio 和 okhttp 一樣,都會(huì)有一個(gè)請(qǐng)求攔截器和響應(yīng)攔截器,通過攔截器,我們可以在請(qǐng)求之前或響應(yīng)之后做一些同意的預(yù)處理。例如我們發(fā)起請(qǐng)求前查看我們請(qǐng)求的參數(shù)和頭部,響應(yīng)的時(shí)候,我們可以查看返回來的數(shù)據(jù)。
Dio dio = new Dio(); // 添加攔截器 if (Config.DEBUG) { dio.interceptors.add(InterceptorsWrapper( onRequest: (RequestOptions options){ print("\n================== 請(qǐng)求數(shù)據(jù) =========================="); print("url = ${options.uri.toString()}"); print("headers = ${options.headers}"); print("params = ${options.data}"); }, onResponse: (Response response){ print("\n================== 響應(yīng)數(shù)據(jù) =========================="); print("code = ${response.statusCode}"); print("data = ${response.data}"); print("\n"); }, onError: (DioError e){ print("\n================== 錯(cuò)誤響應(yīng)數(shù)據(jù) ======================"); print("type = ${e.type}"); print("message = ${e.message}"); print("stackTrace = ${e.stackTrace}"); print("\n"); } )); }
如果我們想要移除攔截器,那么我們可以將其設(shè)置為 null
dio.interceptor.request.onSend=null; dio.interceptor.response.onSuccess=null; dio.interceptor.response.onError=null;
token 添加
// 頭部添加 token 驗(yàn)證 headers["Authorization"] = "token lskjdlklsjkdklsjd333"; option.headers = headers; ///超時(shí) option.connectTimeout = 15000; try { Response response = await dio.request(url, data: params, options: option); } on DioError catch (e) { // 請(qǐng)求錯(cuò)誤處理 }
自動(dòng)生成 dart 的 json 實(shí)體類插件 FlutterJsonBeanFactory
在 Android 開發(fā)中,有 GsonFormat 這個(gè)插件來講 json 數(shù)據(jù)自動(dòng)轉(zhuǎn)化成 Bean;那么在 Flutter 中也有類似的插件可以生產(chǎn)序列化的實(shí)體類的插件:FlutterJsonBeanFactory
step 1:下載插件 FlutterJsonBeanFactory,安裝完成后重啟
Setting -> Plugins -> Browse Respositories 中搜索 FlutterJsonBeanFactory
step 2:創(chuàng)建實(shí)體類,在指定目錄下:
New -> dart bean class File from JSON
step 3:輸入實(shí)體類名及 json 格式的數(shù)據(jù)
step 4:最后生成的實(shí)體類: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; } }
請(qǐng)求錯(cuò)誤處理
Response response; try { response = await dio.request(url, data: params, options: option); } on DioError catch (e) { // 請(qǐng)求錯(cuò)誤處理 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('請(qǐng)求異常: ' + e.toString()); print('請(qǐng)求異常 url: ' + url); } return new ResultData(Code.errorHandleFunction(errorResponse.statusCode, e.message, noTip), false, errorResponse.statusCode); }
其中 ResultData 是網(wǎng)絡(luò)結(jié)果處理的實(shí)體類
/** * 網(wǎng)絡(luò)結(jié)果數(shù)據(jù) * 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 是處理網(wǎng)絡(luò)錯(cuò)誤的編碼,并將錯(cuò)誤結(jié)果通過 eventbus 發(fā)送出去,一般我們可以在 main_pager 中注冊(cè)監(jiān)聽這個(gè)事件。
///網(wǎng)絡(luò)請(qǐng)求錯(cuò)誤編碼 class Code { ///網(wǎng)絡(luò)錯(cuò)誤 static const NETWORK_ERROR = -1; ///網(wǎng)絡(luò)超時(shí) static const NETWORK_TIMEOUT = -2; ///網(wǎng)絡(luò)返回?cái)?shù)據(jù)格式化一次 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; } }
完成的網(wǎng)絡(luò)請(qǐng)求類: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請(qǐng)求管理類,可單獨(dú)抽取出來 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)); } ///發(fā)起網(wǎng)絡(luò)請(qǐng)求 ///[ url] 請(qǐng)求url ///[ params] 請(qǐng)求參數(shù) ///[ header] 外加頭 ///[ option] 配置 static request(url, params, Map<String, String> header, Options option, {noTip = false}) async { //沒有網(wǎng)絡(luò) 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); } //授權(quán)碼 if (optionParams["authorizationCode"] == null) { var authorizationCode = await getAuthorization(); if (authorizationCode != null) { optionParams["authorizationCode"] = authorizationCode; } } headers["Authorization"] = optionParams["authorizationCode"]; // 設(shè)置 baseUrl if (option != null) { option.headers = headers; } else{ option = new Options(method: "get"); option.headers = headers; } ///超時(shí) option.connectTimeout = 15000; Dio dio = new Dio(); // 添加攔截器 if (Config.DEBUG) { dio.interceptors.add(InterceptorsWrapper( onRequest: (RequestOptions options){ print("\n================== 請(qǐng)求數(shù)據(jù) =========================="); print("url = ${options.uri.toString()}"); print("headers = ${options.headers}"); print("params = ${options.data}"); }, onResponse: (Response response){ print("\n================== 響應(yīng)數(shù)據(jù) =========================="); print("code = ${response.statusCode}"); print("data = ${response.data}"); print("\n"); }, onError: (DioError e){ print("\n================== 錯(cuò)誤響應(yīng)數(shù)據(jù) ======================"); 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) { // 請(qǐng)求錯(cuò)誤處理 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('請(qǐng)求異常: ' + e.toString()); print('請(qǐng)求異常 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); } ///清除授權(quán) static clearAuthorization() { optionParams["authorizationCode"] = null; SpUtils.remove(Config.TOKEN_KEY); } ///獲取授權(quán)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) { //提示輸入賬號(hào)密碼 } else { //通過 basic 去獲取token,獲取到設(shè)置,返回token return "Basic $basic"; } } else { optionParams["authorizationCode"] = token; return token; } } }
使用示例
/// 登錄 model class LoginModel{ // 手機(jī)號(hào)碼登錄 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); } } // 獲取驗(yàn)證碼 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); } } }
看完上述內(nèi)容,你們對(duì)使用Flutter怎么封裝一個(gè)網(wǎng)絡(luò)請(qǐng)求框架有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。