<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      flutter 網(wǎng)絡請求Dio封裝

      封裝網(wǎng)絡請求的幾個好處:
      1、便于統(tǒng)一配置請求參數(shù),如header,公共參數(shù),加密規(guī)則等
      2、方便調試,日志打印
      3、優(yōu)化代碼性能,避免到處濫new對象,構建全局單例
      4、簡化請求步驟,只暴露需要的響應數(shù)據(jù),而對錯誤的響應統(tǒng)一回調
      5、對接口數(shù)據(jù)的基類封裝,簡化解析流程

      添加依賴:
      dependencies:
        dio: ^3.0.10

      pub地址:https://pub.flutter-io.cn/packages/dio


      效果示例:

       

      首先創(chuàng)建一個Dio實體類dioUtils.dart

      代碼如下,盡量詳細明了的解釋。

      import 'package:connectivity/connectivity.dart'; //網(wǎng)絡監(jiān)聽組件
      import 'package:cookie_jar/cookie_jar.dart';
      import 'package:dio/dio.dart';
      import 'package:dio_cookie_manager/dio_cookie_manager.dart';
      import 'package:flutter_app/http/error_handle.dart';
      import 'package:flutter_app/route/application.dart';
      //規(guī)定函數(shù)類型
      typedef BackError = Function(int code, String msg);
      class DioUtil{
        Dio dio;
      
        //服務器ip
        static final String BASEURL = '';
      
        //普通格式的header
        static final Map<String, dynamic> headers = {
          "Accept":"application/json",
        "Content-Type":"application/x-www-form-urlencoded",
        };
      //json格式的header
        static final Map<String, dynamic> headersJson = {
          "Accept":"application/json",
          "Content-Type":"application/json; charset=UTF-8",
        };
      
        //構造實體類
        DioUtil(){
          
          BaseOptions options = BaseOptions();
          //注冊請求服務器
          options.baseUrl = BASEURL;
          //設置連接超時單位毫秒
          options.connectTimeout=5000;
          //  響應流上前后兩次接受到數(shù)據(jù)的間隔,單位為毫秒。如果兩次間隔超過[receiveTimeout],
          //  [Dio] 將會拋出一個[DioErrorType.RECEIVE_TIMEOUT]的異常.
          //  注意: 這并不是接收數(shù)據(jù)的總時限.
          options.receiveTimeout=3000;
          //設置請求超時單位毫秒
          options.sendTimeout = 5000;
          //如果返回數(shù)據(jù)是json(content-type),
          // dio默認會自動將數(shù)據(jù)轉為json,
          // 無需再手動轉](https://github.com/flutterchina/dio/issues/30)
          options.responseType = ResponseType.plain;
          options.headers = headers;
      
          dio = Dio(options);
          dio.interceptors.add(CookieManager(CookieJar()));
        }
      
        void get(String url,{Map<String,dynamic> data,success,BackError error}) async{
          _isNet(error);
          ApplicationPrint.printInfo('get請求啟動! url:$url ,body: $data');
          Response response;
          try{
            if(data==null){
              response = await dio.get(url);
            }else{
              response = await dio.get(url,queryParameters:data);
            }
      
            if(response.statusCode == ExceptionHandle.success){
                success(response.data);
            }else{
                error(response.statusCode,"數(shù)據(jù)服務出現(xiàn)異常!");
            }
          }on DioError catch (e){
      
            final NetError netError = ExceptionHandle.handleException(e);
            error(netError.code,netError.msg);
            ApplicationPrint.printInfo('get請求發(fā)生錯誤:${netError.code}//${netError.msg}');
          }
        }
      
        void post(String url,{Map<String,dynamic> data,success,BackError error})async{
          ApplicationPrint.printInfo('post請求啟動! url:$url ,body: $data');
          Response response;
          try{
            if(data==null){
              response = await dio.post(url);
            }else{
              response = await dio.post(url,data: data);
            }
      
            if(response.statusCode == 200){
              success(response.data);
            }else{
              error(response.statusCode,"數(shù)據(jù)服務出現(xiàn)異常!");
            }
          }on DioError catch (e){
            final NetError netError = ExceptionHandle.handleException(e);
            error(netError.code,netError.msg);
            ApplicationPrint.printInfo('post請求發(fā)生錯誤:${netError.code}//${netError.msg}');
          }
        }
      
        // 判斷是否有網(wǎng)
        void _isNet(BackError error) async{
          //沒有網(wǎng)絡
          var connectivityResult = await (Connectivity().checkConnectivity());
          if (connectivityResult == ConnectivityResult.none) {
            error(ExceptionHandle.net_error, '網(wǎng)絡異常,請檢查你的網(wǎng)絡!');
            return;
          }
        }
      
      }

      寫一個全局靜態(tài)文件,實例化一次。application.dart

      class ApplicationPrint{
        //全局日志打印設置 true 顯示,false顯示
        static const bool _IsPrint = true;
        static void printInfo(info){
          if(_IsPrint){
            print(info);
          }
        }
      }
      
      
      class ApplicationDioUtils{
        static DioUtil dioUtil; //注冊全局dio
      }

      在程序啟動時初始化。

      void main() {
        final router =FluroRouter();
        Routes.configureRouts(router);
        ApplicationRouter.router = router;
      
        ApplicationDioUtils.dioUtil = DioUtil();//實例化dio
      
        runApp(MyApp());
      }

      發(fā)送網(wǎng)絡請求:post and get

       _getUser(){
          var url = 'https://www.fastmock.site/mock/a8fefcc6e356f684b8f80984543c1cac/flutter/getUserInfo';
        //get 請求,參數(shù)為 請求地址、請求參數(shù)、請求成功之后的回調,以及出現(xiàn)異常時的監(jiān)聽 ApplicationDioUtils.dioUtil.get(url,data:{
      "user":"zhangsan"},success: (data){ ApplicationPrint.printInfo("_getUser"); ApplicationPrint.printInfo(data); ApplicationPrint.printInfo(data is Map); ApplicationPrint.printInfo(data is String); String name = '{"":""}'; ApplicationPrint.printInfo(data is String); ApplicationPrint.printInfo( jsonDecode(data)['age']); },error: (code,msg){ ApplicationPrint.printInfo('請求出現(xiàn)異常$code'); }); } _getUser2(){ var url = 'https://www.fastmock.site/mock/a8fefcc6e356f684b8f80984543c1cac/flutter/postInfo';
      ApplicationDioUtils.dioUtil.post(url,data:{
      'user':'name','pwd':123},success: (data){ ApplicationPrint.printInfo("_getUser2"); ApplicationPrint.printInfo(data); ApplicationPrint.printInfo(data is Map); ApplicationPrint.printInfo(data is String); json.decode(data.toString()); // ApplicationPrint.printInfo( jsonDecode(data)[0]['name']); ApplicationPrint.printInfo( json.decode(data.toString())); },error: (code,msg){ ApplicationPrint.printInfo('請求出現(xiàn)異常$code'); }); }

      本猿是個新手,決定吧現(xiàn)在學習的每一步都記錄下來,也都盡量寫一些方便擴展,思路明晰的封裝包,不為別的,只為日后自己可以看懂(哈哈)。

      所以有什么不對的地方大家可以指出,不要噴,有話好好說(嘿)!

       

      posted @ 2021-01-20 10:10  淡然吖  閱讀(4606)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 亚洲国产成人精品无码区蜜柚 | 国产成人理论在线视频观看| 国产精品一区二区三区四区| 日韩av综合中文字幕| 狂野欧美性猛交免费视频| 无码丰满人妻熟妇区| 亚洲成人免费一级av| 欧美黑吊大战白妞| 草裙社区精品视频播放| 一区二区三区国产亚洲网站| 日本一道一区二区视频| 中文字幕无码免费久久 | 九九热这里只有精品在线| 亚洲欧美日韩高清一区二区三区| 亚洲中文字幕人成影院| 亚洲码亚洲码天堂码三区| www国产无套内射com| 欧美成人va免费大片视频| 婷婷久久香蕉五月综合加勒比| 亚洲人成网站999久久久综合| 亚欧成人精品一区二区乱| 国产精品自拍中文字幕| 少妇被爽到高潮喷水久久欧美精品| 成人综合人人爽一区二区| 无套内射视频囯产| 无码国产精品一区二区免费虚拟vr| 不卡在线一区二区三区视频| 久在线精品视频线观看| 顺义区| 国产精品老熟女一区二区| 丁香婷婷色综合激情五月| 宝山区| 国产中文字幕在线一区| 亚洲欧美中文字幕日韩一区二区| 亚洲男人的天堂久久香蕉| 日韩永久永久永久黄色大片| 亚洲老熟女一区二区三区 | 免费天堂无码人妻成人av电影| 99国精品午夜福利视频不卡99| 欧美日韩精品一区二区视频| 欧美日韩一线|