Flutter -------- Http库 网络请求封装(HttpController)
http库 再次封装的网络请求类 HttpController
1.添加依赖
dependencies:
http: ^0.12.0 #latest version
2.导入库
import 'package:http/http.dart' as http; //导入前需要配置
效果图:

封装类
import 'package:http/http.dart' as http;
class HttpController {
static void get(String url, Function callback,
{Map<String, String> params, Function errorCallback}) async {
if (params != null && params.isNotEmpty) {
StringBuffer sb = new StringBuffer("?");
params.forEach((key, value) {
sb.write("$key" + "=" + "$value" + "&");
});
String paramStr = sb.toString();
paramStr = paramStr.substring(0, paramStr.length - 1);
url += paramStr;
}
try {
http.Response res = await http.get(url);
if (callback != null) {
callback(res.body);
}
} catch (exception) {
if (errorCallback != null) {
errorCallback(exception);
}
}
}
static void post(String url, Function callback,
{Map<String, String> params, Function errorCallback}) async {
try {
http.Response res = await http.post(url, body: params);
if (callback != null) {
callback(res.body);
}
} catch (e) {
if (errorCallback != null) {
errorCallback(e);
}
}
}
}
调用:
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter_test1/http/HttpController.dart'; class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return new Page();
}
} class Page extends State<HomePage> {
String dataStr = "";
var _items = []; @override
Widget build(BuildContext context) {
return layout(context);
} @override
void initState() {
super.initState();
getData();
} void getData() {
Map<String, String> map = new Map();
map["v"] = "1.0";
map["month"] = "7";
map["day"] = "25";
map["key"] = "bd6e35a2691ae5bb8425c8631e475c2a";
HttpController.post("http://api.juheapi.com/japi/toh", (data) {
if (data != null) {
final body = json.decode(data.toString());
final feeds = body["result"];
var items = [];
feeds.forEach((item) {
items.add(Model(item["_id"], item["title"], item["pic"], item["year"],
item["month"], item["day"], item["des"], item["lunar"]));
});
setState(() {
dataStr = data.toString();
_items = items;
});
}
}, params: map);
} Widget layout(BuildContext context) {
return new Scaffold(
appBar: buildAppBar(context),
body:
new ListView.builder(itemCount: _items.length, itemBuilder: itemView),
);
} Widget itemView(BuildContext context, int index) {
Model model = this._items[index];
//设置分割线
if (index.isOdd) return new Divider(height: 2.0);
return new Container(
color: Color.fromARGB(0x22, 0x49, 0xa9, 0x8d),
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Text('${model.year}年${model.month}月${model.day}日',
style: new TextStyle(fontSize: 15.0)),
new Text('(${model.lunar})',
style: new TextStyle(fontSize: 15.0)),
],
),
new Center(
heightFactor: 6.0,
child: new Text("${model.title}",
style: new TextStyle(fontSize: 17.0)),
)
],
))));
} Widget buildAppBar(BuildContext context) {
return new AppBar(title: const Text('历史今日'));
}
} class Model {
String _id;
String title;
String pic;
int year;
int month;
int day;
String des;
String lunar; Model(this._id, this.title, this.pic, this.year, this.month, this.day,
this.des, this.lunar);
}
Flutter -------- Http库 网络请求封装(HttpController)的更多相关文章
- flutter dio网络请求封装实现
flutter dio网络请求封装实现 文章友情链接: https://juejin.im/post/6844904098643312648 在Flutter项目中使用网络请求的方式大致可分为两种 ...
- Flutter学习(7)——网络请求框架Dio简单使用
原文地址: Flutter学习(7)--网络请求框架Dio简单使用 | Stars-One的杂货小窝 Flutter系列学习之前都是在个人博客发布,感兴趣可以过去看看 网络请求一般APP都是需要的,在 ...
- React-Native 之 GD (八)GET 网络请求封装
1.到这里,相信各位对 React-Native 有所熟悉了吧,从现在开始我们要慢慢往实际的方向走,这边就先从网络请求这部分开始,在正式开发中,网络请求一般都单独作为一部分,我们在需要使用的地方只需要 ...
- 十. Axios网络请求封装
1. 网络模块的选择 Vue中发送网络请求有非常多的方式,那么在开发中如何选择呢? 选择一:传统的Ajax是基于XMLHttpRequest(XHR) 为什么不用它呢?非常好解释配置和调用方式等非常混 ...
- Spring Boot + Vue 前后端分离开发,前端网络请求封装与配置
前端网络访问,主流方案就是 Ajax,Vue 也不例外,在 Vue2.0 之前,网络访问较多的采用 vue-resources,Vue2.0 之后,官方不再建议使用 vue-resources ,这个 ...
- 移动开发在路上-- IOS移动开发 五 网络请求封装
接着上次的讲,这次我们讲 网络请求的封装 打开创建的项目,让我们一起来继续完成他, 上次我们说到GET请求地址的拼接: 我们接着上次的继续完善: 下边我们要定义的是 block //定义block ...
- iOS开发——post异步网络请求封装
IOS中有许多网络请求的函数,同步的,异步的,通过delegate异步回调的. 在做一个项目的时候,上网看了很多别人的例子,发现都没有一个简单的,方便的异步请求的封装例子.我这里要给出的封装代码,是异 ...
- Android项目开发全程(三)-- 项目的前期搭建、网络请求封装是怎样实现的
在前两篇博文中已经做了铺垫,下面咱们就可以用前面介绍过的内容开始做一个小项目了(项目中会用到Afinal框架,不会用Afinal的童鞋可以先看一下上一篇博文),正所谓麻雀虽小,五脏俱全,这在里我会尽量 ...
- React Native 网络请求封装:使用Promise封装fetch请求
最近公司使用React作为前端框架,使用了异步请求访问,这里做下总结: React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XMLHttpRe ...
随机推荐
- ISCC之msc4
一开始拿到题目的时候有点懵的,给个gif图片,233 常规分析,gif打开看一看,发现gif是多张图片组成,于是用fastStone Image viewer分解成九张图片 后面用PS拼起来,得到一个 ...
- PAT_A1059
这是一道素数因子分解的问题: 1.先打印素数表出来,以便后期使用,素数表的大小就是10^5级别就可以,因为输入的数是long int(即就是int而已),大小最大21亿(10^10量级的),我们这里素 ...
- vue-(过滤器,钩子函数,路由)
1.局部过滤器 在当前组件内部使用过滤器,修饰一些数据 //声明 filters:{ '过滤器的名字':function(val,a,b){ //a 就是alax ,val就是当前的数据 } } // ...
- java中List集合
List集合是一个元素有序可以重复的集合,集合中每个元素都有其对应的顺序索引.List集合允许使用重复集合,前面博客写到Set不允许有重复集合.List集合可以通过索引来访问指定位置的集合元素. Li ...
- 用python爬取全网妹子图片【附源码笔记】
这是晚上没事无聊写的python爬虫小程序,专门爬取妹子图的,养眼用的,嘻嘻!身为程序狗只会这个了! 废话不多说,代码附上,仅供参考学习! """ 功能:爬取妹子图全网妹 ...
- css Grid布局
CSS Grid 布局完全指南(图解 Grid 详细教程)https://www.html.cn/archives/8510#prop-grid-gap 5分钟学会 CSS Grid 布局https: ...
- C#向数据库中插入或更新null空值
一.在SQL语句中直接插入null或空字符串“” int? item = null; item == null ? "null" : item.ToString(); item = ...
- C语言蓝桥杯比赛原题和解析
蓝桥杯:在计算机编程领域,是具有一定含金量的竞赛,用于选拔信息技术人才. 一般分为多个领域,其中包含了C/C#/C++/Java/Python等编程语言的测试题,多为算法的设计题. 下面,在搜题过程中 ...
- 一个关于gcd的等式的证明
证:$a > b$ 且 $gcd(a,b)=1$,有 $gcd(a^n-b^n, a^m-b^m) = a^{gcd(n, m)} - b^{gcd(n,m)}$. 证明: 假设 $n > ...
- mui真机调试时无法查找到手机
在做mui的真机调试时手机查找不到手机,有可能是你的 adb interface 驱动 版本过低,先下载 或自行百度 http://www.onlinedown.net/soft/1164054. ...