zoukankan      html  css  js  c++  java
  • 【dart学习】-- Dart之网络请求操作

    Flutter的请求网络有多种方式,一种是使用dart io中的HttpClient发起的请求,一种是使用dio库,另一种是使用http库,先学一下getpostputdelete就等后面用到在学。下面就实践:

    1.dart io发起的请求

    1.1.get请求 

    import 'dart:io';//导IO包
    import 'dart:convert';//解码和编码JSON
    void main() {
      _get();
    }
    
    _get() async{
      var responseBody;
      //1.创建HttpClient
      var httpClient = new HttpClient();
      //2.构造Uri
      var requset = await httpClient.getUrl(Uri.parse("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1"));
      //3.关闭请求,等待响应
      var response = await requset.close();
      //4.进行解码,获取数据
      if(response.statusCode == 200){
          //拿到请求的数据
          responseBody = await response.transform(utf8.decoder).join();
          //先不解析打印数据
          print(responseBody);
      }else{
        print("error");
      }
      
    }

    结果如下:

    {"error":false,
      "results":[
      {"_id":"5ccdbc219d212239df927a93","createdAt":"2019-05-04T16:21:53.523Z","desc":"2019-05-05","publishedAt":"2019-05-04T16:21:59.733Z","source":"web","type":"u798fu5229","url":"http://ww1.sinaimg.cn/large/0065oQSqly1g2pquqlp0nj30n00yiq8u.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5cc43919fc3326376038d233","createdAt":"2019-04-27T19:12:25.536Z","desc":"2019-04-27","publishedAt":"2019-04-27T19:12:51.865Z","source":"web","type":"u798fu5229","url":"https://ww1.sinaimg.cn/large/0065oQSqly1g2hekfwnd7j30sg0x4djy.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5c6a4ae99d212226776d3256","createdAt":"2019-02-18T06:04:25.571Z","desc":"2019-02-18","publishedAt":"2019-04-10T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqly1g0ajj4h6ndj30sg11xdmj.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5c2dabdb9d21226e068debf9","createdAt":"2019-01-03T06:29:47.895Z","desc":"2019-01-03","publishedAt":"2019-01-03T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqly1fytdr77urlj30sg10najf.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5c25db189d21221e8ada8664","createdAt":"2018-12-28T08:13:12.688Z","desc":"2018-12-28","publishedAt":"2018-12-28T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqly1fymj13tnjmj30r60zf79k.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5c12216d9d21223f5a2baea2","createdAt":"2018-12-13T09:07:57.2Z","desc":"2018-12-13","publishedAt":"2018-12-13T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fy58bi1wlgj30sg10hguu.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5bfe1a5b9d2122309624cbb7","createdAt":"2018-11-28T04:32:27.338Z","desc":"2018-11-28","publishedAt":"2018-11-28T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fxno2dvxusj30sf10nqcm.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5bf22fd69d21223ddba8ca25","createdAt":"2018-11-19T03:36:54.950Z","desc":"2018-11-19","publishedAt":"2018-11-19T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fxd7vcz86nj30qo0ybqc1.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5be14edb9d21223dd50660f8","createdAt":"2018-11-06T08:20:43.656Z","desc":"2018-11-06","publishedAt":"2018-11-06T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fwyf0wr8hhj30ie0nhq6p.jpg","used":true,"who":"lijinshanmx"},
      {"_id":"5bcd71979d21220315c663fc","createdAt":"2018-10-22T06:43:35.440Z","desc":"2018-10-22","publishedAt":"2018-10-22T00:00:00.0Z","source":"web","type":"u798fu5229","url":"https://ws1.sinaimg.cn/large/0065oQSqgy1fwgzx8n1syj30sg15h7ew.jpg","used":true,"who":"lijinshanmx"}
    ]
    }

    1.2.post请求

    _post() async{
      var responseBody;
      //1.创建HttpClient
      var httpClient = new HttpClient();
      //2.构造Uri
      var requset = await httpClient.postUrl(Uri.parse("http://www.wanandroid.com/user/login?username=1&password=123456"));
      //3.关闭请求,等待响应
      var response = await requset.close();
      //4.进行解码,获取数据
      if(response.statusCode == 200){
      //拿到请求的数据
      responseBody = await response.transform(utf8.decoder).join();
      //先不解析打印数据
        print(responseBody);
      }else{
        print("error");
      }
    
    }

    返回结果如下:

    {
        ‘data’:null, ‘errorCode’: -1, ‘errorMsg’:’账号密码不匹配!’
    }

    2.dio请求

    dio是一个强大的Dart Http请求库,支持Restful APIFormData、拦截器、错误处理、转换器、设置Http代理、请求取消、Cookie管理、文件上传和下载、超时等。在pub.flutter-io.cn/packages搜最新的依赖包,这个网址太好用,你想搜一些三方库里面都有:

     

    pubspec.yaml添加依赖:
    dio: ^2.0.14

    导入依赖:

    import 'package:dio/dio.dart';

    2.1.get请求

    //dio get请求
    dio_get() async{
      try{
          Response response;
          //等待返回response
          response = await Dio().get("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1");
          if(response.statusCode == 200){
            print(response);
          }else{
            print("error");
          }
      }catch(e){
         print(e);
    
      }
    }

    2.2.post请求

    dio_post() async{
      try{
        Response response;
        response = await Dio().post("http://www.wanandroid.com/user/login?username=1&password=123456");
        if(response.statusCode == 200){
          print(response);
        }else{
          print("error");
        }
      }catch(e){
        print(e);
      }
    }

    效果同样是ok的。

    3.http库

    继续去上面链接搜最新的包,是http 0.12.0+1,在pubspec.yaml下添加依赖,在文件导入包:

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

    上面这次导入库的方式有一点点区别,多了as这个关键字,这是什么意思呢?通过as是为了解决变量名冲突的方法,因为导入不同的库有可能遇到不同库之间因为导入变量名冲突的问题。

    3.1.get请求

    //http库的get请求方式
    http_get() async{
      try{
        //因为导入http 用了as xxx方式,所以对象请求都用xxx.get方式
        var response = await my_http.get("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1");
        if(response.statusCode == 200){
          //打印返回的数据
          print(response.body);
        }else{
          print("error");
        }
      }catch(e){
        print(e);
      }
    }

    3.2.post请求

    //http库的post请求方式
    http_post() async{
      try{
        //因为导入http 用了as xxx方式,所以对象请求都用xxx.get方式
        var response = await my_http.post("http://www.wanandroid.com/user/login?username=1&password=123456");
        if(response.statusCode == 200){
          //打印返回的数据
          print(response.body);
        }else{
          print("error");
        }
      }catch(e){
        print(e);
      }
    }

    以上三种库的getpsot方式都实践了一遍,在平时开发中最好用dio库和http库,因为dart io中是使用HttpClient发起的请求,HttpClient本身功能较弱,很多常用功能不支持。

  • 相关阅读:
    hdu 3367 Pseudoforest
    hdu 2489 Minimal Ratio Tree
    hdu 4009 Transfer water
    poj 3164 Command Network
    hdu 3926 Hand in Hand
    hdu 3938 Portal
    5-26日(面经总结)
    5-25日
    5-21日|5-22日
    5-13日记录|5-14日
  • 原文地址:https://www.cnblogs.com/lxlx1798/p/11134306.html
Copyright © 2011-2022 走看看