一、目录

1、访问网页

2、http请求

-----------------------------这是分割线-----------------------------

1、访问网页

基于url_launcher库实现,最新版本号 5.0.2,没有的话需要添加到pubspec.yaml中

然后get 该package

只要引入不报错就ok了。

ex.dart

/*
time: 2019-4-3
*/ // 引入资源包
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart'; // main fun
void main() => runApp(new MyApp()); class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
title: 'url请求',
home: new Scaffold(
appBar: new AppBar(
title: new Text('Example of packages'),
),
body: new Center(
child: new RaisedButton(
onPressed: () {
print();
// url
const url = "https://www.baidu.com";
launch(url); // 打开浏览器跳转至百度网页
},
child: new Text('baidu'),
),
),
));
}
}

运行效果如下

点击baidu按钮,打开网页

2、http请求

2.1、Http请求

下载Http库 版本号 0.12.0+2

ex.dart

/*
time: 2019-4-3
title: htttp请求
*/ // 引入资源包
import 'package:flutter/material.dart';
//import 'package:url_launcher/url_launcher.dart';
import 'package:http/http.dart' as http; // main fun
void main () => runApp(new MyApp()); class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
title: 'Http 请求示例',
home: new Scaffold(
appBar: new AppBar(
title: new Text('Http 请求示例'),
),
body: new Center(
child: new RaisedButton(
onPressed: () {
print();
var url = 'http://www.cnblogs.com/'; // get
http.get(url).then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body[10]}");
}); // post
http.post(url,body: {"key": "value"}).then((response) {
print('this is post');
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
});
},
child: new Text('发送请求'),
),
),
)
);
}
}

运行效果

我点击发送请求之后,控制台输出

请求成功,并返回了内容。

2.1、HttpClient请求

ex.dart

/*
time: 2019-4-3
title: htttpClient请求
*/ // 引入资源包
import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:io'; // main fun
void main () => runApp(new MyApp()); class MyApp extends StatelessWidget {
// 获取天气数据
void getWeatherData() async {
try {
// 实例化一个HttpCLient对象
HttpClient httpClient = new HttpClient(); // 发起请求
HttpClientRequest request = await httpClient.getUrl(
Uri.parse("http://wthrcdn.etouch.cn/weather_mini?city=重庆市")
);
// 等待服务器返回数据
HttpClientResponse response = await request.close();
// 使用utf-8.decoder从response解析数据
var result = await response.transform(utf8.decoder).join();
// 输出
print(result);
// 关闭
httpClient.close(); } catch (e) {
print("请求失败:$e");
}
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
title: 'HttpClient 请求示例',
home: new Scaffold(
appBar: new AppBar(
title: new Text('HttpClient 请求示例'),
),
body: new Center(
child: new RaisedButton(
onPressed: getWeatherData,
child: new Text('获取天气数据'),
),
),
)
);
}
}

运行效果如下:

点击按钮之后,控制台输出:

I/flutter (15353): {"data":{"yesterday":{"date":"6日星期六","high":"高温 30℃","fx":"无持续风向","low":"低温 14℃","fl":"<![CDATA[<3级]]>","type":"晴"},"city":"重庆","forecast":[{"date":"7日星期天","high":"高温 32℃","fengli":"<![CDATA[<3级]]>","low":"低温 15℃","fengxiang":"无持续风向","type":"晴"},{"date":"8日星期一","high":"高温 32℃","fengli":"<![CDATA[<3级]]>","low":"低温 18℃","fengxiang":"无持续风向","type":"多云"},{"date":"9日星期二","high":"高温 29℃","fengli":"<![CDATA[<3级]]>","low":"低温 19℃","fengxiang":"无持续风向","type":"多云"},{"date":"10日星期三","high":"高温 24℃","fengli":"<![CDATA[<3级]]>","low":"低温 18℃","fengxiang":"无持续风向","type":"阴"},{"date":"11日星期四","high":"高温 25℃","fengli":"<![CDATA[<3级]]>","low":"低温 18℃","fengxiang":"无持续风向","type":"多云"}],"ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","wendu":"3}

flutter 访问网页+http请求的更多相关文章

  1. 浅谈 php 采用curl 函数库获取网页 cookie 和 带着cookie去访问 网页的方法!!!!

    由于近段时间帮朋友开发一个能够查询正方教务系统的微信公众平台号.有所收获.这里总结下个人经验. 开讲前,先吐槽一下新浪云服务器,一个程序里的   同一个函数  在PC测试可以正常运行,在它那里就会挂的 ...

  2. 访问网页时提示的503错误信息在IIS中怎么设置

    访问网页时提示的503错误信息在IIS中怎么设置 503是一种常见的HTTP状态码,出现此提示信息的原因是由于临时的服务器维护或者过载,服务器当前无法处理请求则导致了访问网页时出现了503错误.那么当 ...

  3. Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)

    http://blog.csdn.net/yanzi1225627/article/details/22222735 如前文所示的TCP局域网传送东西,除了对传输层的TCP/UDP支持良好外,Andr ...

  4. WiFi-ESP8266入门http(3-4)网页一键配网(1若为普通wifi直连 2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信

    网页一键配网(1若为普通wifi直连  2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信 工程连接:https://github.com/Dongvdong/ESP8266_H ...

  5. Python3 定时访问网页

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50358695 如果我有一组网站,想要定 ...

  6. Python爬虫笔记【一】模拟用户访问之设置请求头 (1)

    学习的课本为<python网络数据采集>,大部分代码来此此书. 网络爬虫爬取数据首先就是要有爬取的权限,没有爬取的权限再好的代码也不能运行.所以首先要伪装自己的爬虫,让爬虫不像爬虫而是像人 ...

  7. apache启动但是无法访问网页

    apache启动但是访问不了的问题排除??? 端口没有被占用,则需要考虑防火墙问题. 首先我们需要确保远程的Linux系统已经安装好,我们使用xshell远程SSH登录到Linux系统里,同时我们也要 ...

  8. 第14.6节 使用Python urllib.request模拟浏览器访问网页的实现代码

    Python要访问一个网页并读取网页内容非常简单,在利用<第14.5节 利用浏览器获取的http信息构造Python网页访问的http请求头>的方法构建了请求http报文的请求头情况下,使 ...

  9. Flutter学习(7)——网络请求框架Dio简单使用

    原文地址: Flutter学习(7)--网络请求框架Dio简单使用 | Stars-One的杂货小窝 Flutter系列学习之前都是在个人博客发布,感兴趣可以过去看看 网络请求一般APP都是需要的,在 ...

随机推荐

  1. Ubuntu 16.04 环境下配置apache2.4 + php5.6

    相信用惯了Windows的朋友一开始接触Linux是很崩溃的,因为很多东西都是通过命令行来完成的,包括安装绝大多数的开发工具以及环境,那么在Ubuntu下其实可以直接通过apt-get指令来安装apa ...

  2. 如何配置Tomcat以使用Apache httpd?

    How to Connect Tomcat 6 to Apache HTTP Server 2 Tomcat can be run as a standalone server. Tomcat can ...

  3. complex类的设计实现

    #include <iostream> #include <cmath> using namespace std; class Complex{ ,); Complex(Com ...

  4. ctrl+c以及写操作失败和flush

    .kill命令可以带信号号码选项,也可以不带.如果没有信号号码,kill命令就会发出终止信号(15),这个信号可以被进程捕获,使得进程在退出之前可以清理并释放资源.也可以用kill向进程发送特定的信号 ...

  5. 实验四 CC2530平台上UART组件的TinyOS编程

    实验四 CC2530平台上UART组件的TinyOS编程 实验目的: 加深和巩固学生对于TinyOS编程方法的理解和掌握 让学生初步掌握CC2530的UART.及其TinyOS编程方法 学生通过本实验 ...

  6. vpdn1

    在使用L2TP协议构建的VPDN典型组网中,包含LAC和LNS两部分. 1.LAC LAC表示L2TP访问集中器(L2TP Access Concentrator),是附属在交换网络上的具有PPP端系 ...

  7. 学习笔记DL003:神经网络第二、三次浪潮,数据量、模型规模,精度、复杂度,对现实世界冲击

    神经科学,依靠单一深度学习算法解决不同任务.视觉信号传送到听觉区域,大脑听学习处理区域学会“看”(Von Melchner et al., 2000).计算单元互相作用变智能.新认知机(Fukushi ...

  8. 网络操作基础(two)

    P106 一.什么是活动目录?活动目录有哪些优点? 二.什么是域.域树.森林? 三.什么是信任?什么是域的方向及传递性? 四.如何管理活动目录的信任与站点? 解答! (一) 1.活动目录:提供了用于存 ...

  9. SQL server 多个字段设为主键

    create table teacher_course( ton char(8) not null, classno char(8) not null, con char(4) not null pr ...

  10. PymongoDB_study

    import pymongo client = pymongo.MongoClient(host='localhost',port=27017)#连接数据库 #db = client.test#指定数 ...