一、目录

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. Python全栈之路----常用模块----hashlib加密模块

    加密算法介绍 HASH       Python全栈之路----hash函数 Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列 ...

  2. Python学习笔记,day1

    Python学习第一天 一.变量 变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名['and', 'as', 'assert', ...

  3. 导入到eclipse里的工程挺大的,然后就一直报: An internal error occurred during: "Building workspace". GC overhead limit exceeded 这个错误。

    解决方法: 原因是Eclipse默认配置内存太小需要更改Eclipse安装文件夹下的eclipse.ini文件. Eclipse.ini默认文件如下: 修改如下: -Xms1024m -Xmx2048 ...

  4. 17python-BS编程

    1.前端概述(1)上网:就是下载网页(2)浏览器:就是一个解释器2.BS模式的了解(1)BS模式:-----b:browser(浏览器)   s:server(服务端)(2)BS模式运行过程:brow ...

  5. 几何概型 uva11722

    #include<bits/stdc++.h> using namespace std; int t1,t2,s1,s2,w; int get(int b) { ; int d=s2-s1 ...

  6. 火狐firebug和firepath插件安装方法(最新)

    火狐浏览器最近下掉了firebug和firepath插件,用户即使下载了火狐55以下的版本,也无法查找到这两个插件. 因此,可以用以下方法来获取这两个插件. 1.下载火狐55以内版本安装包,安装时迅速 ...

  7. 我发起了一个 用 C# 写 的 浏览器 开源项目 HtmlCore

    我之前还发起过一个 项目, 名字也叫 HtmlCore, 见 <我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 HtmlCore>  https://www.cnblo ...

  8. nvm 知识点

    事项 作用 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash 安装nvm comm ...

  9. 在VS2013、VS2015下如何配置DirectX SDK的开发环境

    在Visual Studio 2013下配置DirectX SDK可以进行基于DirectX的3D大型应用程序的开发.如果在开发DirectX程序时不配置其开发环境会引起编译器报错, 下面就与大家分享 ...

  10. Java递归:一个NB自慰的操作

    递归的核心思想:自身调用自身 示例一:求5的阶乘 常规方法: 使用while循环 1 public class Demo01 { 2 3 public static void main(String[ ...