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 ...
随机推荐
- USB之基本协议和数据波形1
============= 本系列参考 ============= <圈圈教你玩USB>.<Linux那些事儿之我是USB> 协议文档:https://www.usb.or ...
- Mongodb的主从复制
主从服务器的实现原理 首先,主节点会把本服务的与写有关的操作记录下来,读操来不记录,这些操作就记录在local数据库中的oplog.$admin这个集合中,这是一个固定集合,大小是可以配置的,主要是通 ...
- python中的嵌套类
python中的嵌套类 在.NET和JAVA语言中看到过嵌套类的实现,作为外部类一个局部工具还是很有用的,今天在python也看到了很不错支持一下.动态语言中很好的嵌套类的实现,应该说嵌套类解决设计问 ...
- 51nod 1657 电子龟
电子龟的行动,是沿着直线左右走动的.他能够接受两种指令,“T”(向后转,即如果面向左,改成向右:否则就向左)和“F”(向当前面朝的方向往前移动一个单位距离). 现在给出一串指令,让电子龟来执行.你必须 ...
- 学习markdown(一)
转:https://www.jianshu.com/p/81e1608ea2d8 ----------------------------------------------------------- ...
- Spring中Model,ModelMap和ModelAndView
目录 1.Model接口 2.ModelMap 3.ModelAndView 1.Model接口(org.springframework.ui.Model) Model是一个接口,包含addAttri ...
- 自定义创建vue文件代码块
"vue-component": { "prefix": "vue-component", "body": [ &quo ...
- ASP.NET MVC 5 入门-2控制器、路由
一.创建项目: 上起始页,选择新项目. 在中新的项目对话框中,右侧语言类别选择C# ,然后项目类型选择Web,然后选择ASP.NET Web 应用程序 (.NET Framework) 项目模板. 将 ...
- 5、Hadoop 2.6.5 环境搭建
下载 地址:http://archive.apache.org/dist/hadoop/common/ sudo wget http://archive.apache.org/dist/hadoop/ ...
- 洛谷 4290 [HAOI2008]玩具取名 题解
P4290 [HAOI2008]玩具取名 题目描述 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后他会根据自己的喜好,将名字中任意一个字母用&qu ...