【dart学习】-- Dart之网络请求操作
Flutter
的请求网络有多种方式,一种是使用dart io
中的HttpClient
发起的请求,一种是使用dio
库,另一种是使用http
库,先学一下get
和post
,put
、delete
就等后面用到在学。下面就实践:
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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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":"\u798f\u5229","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 API
、FormData
、拦截器、错误处理、转换器、设置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);
}
}
以上三种库的get
和psot
方式都实践了一遍,在平时开发中最好用dio
库和http
库,因为dart io
中是使用HttpClient
发起的请求,HttpClient
本身功能较弱,很多常用功能不支持。
【dart学习】-- Dart之网络请求操作的更多相关文章
- 【转】python3 urllib.request 网络请求操作
python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...
- python3 urllib.request 网络请求操作
python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...
- Flutter学习五之网络请求和轮播图的实现
上期讲到了,怎样实现一个下拉刷新和加载更多的列表,数据更新,需要使用到网络请求,在flutter中,怎样实现一个网络请求呢?官方使用的是dart io中的HttpClient发起的请求,但HttpCl ...
- Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie
1. 引入所需网络请求类: var URL = plus.android.importClass("java.net.URL"); var URLConnection = plus ...
- python学习笔记:网络请求——urllib模块
python操作网络,也就是打开一个网站,或者请求一个http接口,可以使用urllib模块.urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib模 ...
- python3.5.3rc1学习十:网络请求
#sys模块import sys sys.stderr.write('This is stderr text\n')# 因为从定向有缓冲区,所以需要以下这行代码sys.stderr.flush()sy ...
- python学习笔记:网络请求——requests模块
上面讲过的urllib模块太麻烦了,还有一个比较方便的模块,就是requests模块,好用到你怀疑人生·^_^,一定要会哦 需要安装,pip install requests即可,下面是request ...
- 【安卓网络请求开源框架Volley源码解析系列】定制自己的Request请求及Volley框架源码剖析
通过前面的学习我们已经掌握了Volley的基本用法,没看过的建议大家先去阅读我的博文[安卓网络请求开源框架Volley源码解析系列]初识Volley及其基本用法.如StringRequest用来请求一 ...
- Python 网络请求模块 urllib 、requests
Python 给人的印象是抓取网页非常方便,提供这种生产力的,主要依靠的就是 urllib.requests这两个模块. urlib 介绍 urllib.request 提供了一个 urlopen 函 ...
随机推荐
- iOS系统日历选择问题
参考:https://blog.csdn.net/lg_sun/article/details/78913064 -(NSString *)getTimeToken{ NSDateFormatter ...
- <三剑客> 老三:grep命令用法
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- AGC033 D~F——[ 值放到角标的DP ][ 思路+DP ][ 思路 ]
地址:https://atcoder.jp/contests/agc033/ D Complexity dp[ i ][ j ][ k ][ l ] 表示左上角是 ( i , j ) .右下角是 ( ...
- CSS基础知识复习
1. CSS优先级 标签内部属性 style定义的CSS > 文档内定义的css > 引用外部CSS文件 2. CSS选择器类型 . 标签选择器 . 类选择器(使用.做标识) . ID选择 ...
- PHP之namespace小结
命名空间的使用 在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句.所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前. PHP 命名空间中的类名可以通过三种方 ...
- 「LibreOJ β Round」ZQC 的手办
https://loj.ac/problem/504 一类套路题. 首先这个玩意可以两个logn树套树做.... naive地,把区间内的所有数拿出来放进堆里.不断取出. 太多了. 所以开始只保留那初 ...
- JVM调优(四)——tomcat远程debug
JVM调优(四)--tomcat远程debug tomcat远程debug jdwp协议 使用步骤 登录远程服务器,进入tomcat目录,并打开文件: //tomcat/bin/startup.sh ...
- jenkins构建后操作archive the artfacts的用法
参考: https://blog.csdn.net/liqiangeastsun/article/details/79062806 Jenkins构建完成存档 Archive the artifact ...
- 实用|从0到1 搭建Web性能监控系统
工具介绍 1. Statsd 是一个使用Node开发网络守护进程,它的特点是通过UDP(性能好,及时挂了也不影响主服务)或者TCP来监听各种数据信息,然后发送聚合数据到后端服务进行处理. 常见支持的「 ...
- Linux环境下对大小写敏感,linux环境升级node
linux对大小写敏感 在window下可以正常运行的代码,在linux环境下报错,找不到文件,因为window下对大小写不敏感,linux对大小写敏感 linux环境下node升级 1.安装nvm ...