Flutter之測試Http和HttpClient

1?測試Http和HttpClient

? ?導入包:在pubspec.yaml里面導入

  http: ^0.12.2

? main.dart里面導入

?

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';

?

?

?

?

?

?

?

2 代碼實現


import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';void main() {runApp(MyApp1());
}class MyApp1 extends StatelessWidget {void getWeatherData() async {try {HttpClient httpClient = HttpClient();HttpClientRequest request = await httpClient.getUrl(Uri.parse("http://pv.sohu.com/cityjson?ie=utf-8"));HttpClientResponse response = await request.close();var result =  await response.transform(utf8.decoder).join();print(result);httpClient.close();} catch (e) {print("get data fail $e");} finally {}}@overrideWidget build(BuildContext context) {return MaterialApp(title: 'open url',home: Scaffold(appBar: AppBar(// Here we take the value from the MyHomePage object that was created by// the App.build method, and use it to set our appbar title.title: Text('hello flutter'),),body: Center(child: Column(// Column is also a layout widget. It takes a list of children and// arranges them vertically. By default, it sizes itself to fit its// children horizontally, and tries to be as tall as its parent.//// Invoke "debug painting" (press "p" in the console, choose the// "Toggle Debug Paint" action from the Flutter Inspector in Android// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)// to see the wireframe for each widget.//// Column has various properties to control how it sizes itself and// how it positions its children. Here we use mainAxisAlignment to// center the children vertically; the main axis here is the vertical// axis because Columns are vertical (the cross axis would be// horizontal).mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Text('hello word flutter',),RaisedButton(onPressed: () {const url = 'https://www.baidu.com';http.get(url).then((response) {print("狀態 is ${response.statusCode}");print("內容 is ${response.body}");});},child: Text('test Http'),),RaisedButton(onPressed: getWeatherData,child: Text('test HttpClient get weather'),),],),),),);}
}

?

?

?

?

?

?

3 運行結果

點擊test Http日志打印如下

I/flutter (27404): 狀態 is 200
I/flutter (27404): 內容 is <html>
I/flutter (27404): <head>
I/flutter (27404): 	<script>
I/flutter (27404): 		location.replace(location.href.replace("https://","http://"));
I/flutter (27404): 	</script>
I/flutter (27404): </head>
I/flutter (27404): <body>
I/flutter (27404): 	<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>
I/flutter (27404): </body>
I/flutter (27404): </html>

點擊 test HttpClient get weather日志打印如下

?

I/flutter (27404): var returnCitySN = {"cip": "220.250.29.154", "cid": "350100", "cname": "福建省福州市"};

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/287022.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/287022.shtml
英文地址,請注明出處:http://en.pswp.cn/news/287022.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于zookeeper的solrCloud集群搭建

1.安裝及搭建相關環境 1.1環境準備 centos7,jdk1.8,tomcat8,zookeeper3.4.X,solr4.10.X 鏈接: https://pan.baidu.com/s/1i47IuKd 密碼: emqt 2.zookeeper集群搭建 2.1復制zookeeper [rootMiWiFi-R3-srv ~]# mkdir /usr/local/solr-cloud [rootMiWiFi-R3-srv ~]# cp -r zookee…

【小白必懂】C語言求完全數

注意&#xff1a;學生黨如果存在付費問題可以加我好友&#xff0c;我可以開單篇短時間的免費喲~ 私聊我就好~ 情景再現 &#x1f478;小媛&#xff1a;小C&#xff0c;你知道什么是完全數嗎&#xff1f; &#x1f430;小C&#xff1a;知道呀&#xff0c;難道是今天老師又出題…

【三維激光掃描】第四章:點云數據處理

第一節 點云數據處理流程 由于外業獲取點云數據時的多種因素影響,點云數據質量直接影響到三維建模等方面的應用,點云數據處理環節非常重要。本章主要介紹數據處理流程,數據的配準:濾波、縮減、分割、分類,最后介紹點云數據應用。 5.1 數據處理流程 5.1.1 數據處理軟件 …

臺式計算機硬件輸入設備,臺式電腦硬件配置有哪些

臺式電腦硬件配置你知道有哪些?電腦的配置一般是指電腦的硬件配件的高檔程度、性價比等&#xff0c;電腦的性能好壞主要決定于以下主要硬件配置。一起來看看臺式電腦硬件配置有哪些&#xff0c;歡迎查閱!組裝臺式電腦配置1、實用性機型建議&#xff1a;首選1&#xff1a;intel…

mysql 如何用一條SQL將一張表里的數據插入到另一張表 3個例子

1. 表結構完全一樣 insert into 表1select * from 表2 2. 表結構不一樣&#xff08;這種情況下得指定列名&#xff09; insert into 表1 (列名1,列名2,列名3)select 列1,列2,列3 from 表2 3、只從另外一個表取部分值 insert into 表1 (列名1,列名2,列名3) values(列1,列2,(sel…

Android WebView和JavaScript交互

JavaScript在現在的網頁設計中用得很多&#xff0c;Android 的WebView可以載入網頁&#xff0c;WebView也設計了與JavaScript通信的橋梁。這篇主要介紹一下WebViewk控件如何和JavaScript進行交互。 WebView: WebView和網頁相關的主要有一下幾個方法&#xff1a;  setWebViewCl…

Microsoft Dev Box 帶來全新云上開發體驗

在 5 月 24 日, 微軟的產品經理 Anthony Cangialosi 在 Azure 社區發布了一篇博客(Introducing Microsoft Dev Box)&#xff0c; 宣布推出 Microsoft Dev Box !這是一種新的云服務&#xff0c;托管在 Microsoft Azure 中&#xff0c;提供了一個開箱即用的開發工作站&#xff0c…

游戲開發如此簡單?我直接創建了一個游戲場景【python 游戲實戰 02】

前言 本系列文章將會以通俗易懂的對話方式進行教學&#xff0c;對話中將涵蓋了新手在學習中的一般問題。此系列將會持續更新&#xff0c;包括別的語言以及實戰都將使用對話的方式進行教學&#xff0c;基礎編程語言教學適用于零基礎小白&#xff0c;之后實戰課程也將會逐步更新…

【三維激光掃描】第五章:基于點云數據的三維建模

第一節 繪制立面圖 1. 打開CAD 2014,新建一個文件,模板選擇acadiso.dwt,如下圖。 2. 點擊插入→創建點云。

Flutter之基本數據類型測試

1、Flutter的數據基本類型 Dart語言里一切皆為對象&#xff0c;所以如果沒有將變初始化,那么它的默認值為null Number(int、doubkle)StringBoolean(bool) List Map2、測試代碼 void testData() {//Number包含了int和doubleint a 4;int b 8;print(a b);int a1;if (a null)…

清北·NOIP2017濟南考前沖刺班 DAY1 morning

立方數(cubic) Time Limit:1000ms Memory Limit:128MB 題目描述 LYK定義了一個數叫“立方數”&#xff0c;若一個數可以被寫作是一個正整數的3次方&#xff0c;則這個數就是立方數&#xff0c;例如1,8,27就是最小的3個立方數。 現在給定一個數P&#xff0c;LYK想要知道這個數…

2020美國紐約大學計算機科學排名,2020美國紐約大學排名第幾

紐約大學在2020年《美國新聞與世界報道》美國全國性大學排名中排名第29名&#xff0c;在2020年QS世界大學排名中排名第39名。紐約大學專業排名QS世界大學生命科學與醫學專業排名 2020年 第40名QS世界大學醫學專業排名 2020年 第34名QS世界大學牙科專業排名 2020年 第14名QS世界…

saltstack 安裝nginx

1. 目錄結構[rootqing salt]# tree /srv/salt/nginx//srv/salt/nginx/-- config.sls-- files| -- nginx| -- nginx-1.0.15.tar.gz| -- nginx.conf| -- nginx_log_cut.sh| -- nginx-upstream-jvm-route-0.1.tar.gz-- init.sls-- install.sls1 directory, 8 files2. [r…

ArcGIS實驗教程——實驗三十一:ArcGIS構建泰森多邊形(Thiessen Polygon)實例精解

泰森多邊形是進行快速插值和分析地理實體影響區域的常用工具。例如,用離散點的性質描述多邊形區域的性質,用離散點的數據計算泰森多邊形區域的數據。泰森多邊形可用于定性分析、統計分析和臨近分析等。 ArcGIS實驗視頻教程合集:《ArcGIS實驗教程從入門到精通》(附配套實驗…

Python的魔法方法 .

基本行為和屬性 __init__(self[,....])構造函數 . 在實例化對象的時候會自動運行 __del__(self)析構函數 . 在對象被回收機制回收的時候會被調用 __str__(self)輸出函數 . 在實例對象請求輸出的時候會被調用. __repr__(self). 當直接調用實例對象的時候會被調用 __new__(cls,[,…

游戲角色開始動起來了,真帥!【python 游戲實戰 03】

前言 本系列文章將會以通俗易懂的對話方式進行教學&#xff0c;對話中將涵蓋了新手在學習中的一般問題。此系列將會持續更新&#xff0c;包括別的語言以及實戰都將使用對話的方式進行教學&#xff0c;基礎編程語言教學適用于零基礎小白&#xff0c;之后實戰課程也將會逐步更新…

如何讓 ASP.NET Core 支持綁定查詢字符串中的數組

前言有網友在交流群中詢問&#xff0c;如何讓 ASP.NET Core 支持綁定查詢字符串中的數組&#xff1a;據說&#xff0c;在 .NET 7 中已經支持了&#xff1a;但是&#xff0c;在這之前的 .NET 版本能實現相同功能嗎&#xff1f;ByteArrayModelBinder這時&#xff0c;群里的網友提…

Docker Storm開發環境搭建

2019獨角獸企業重金招聘Python工程師標準>>> 1. compose文件 storm-stack.yml version: 3.1services:zookeeper:image: zookeepercontainer_name: zookeeperrestart: alwaysports:- 2181:2181nimbus:image: stormcontainer_name: nimbuscommand: storm nimbusdepend…

Android之解決YouTubePlayerView啟動在Android5.0左右的手機出現奔潰問題

1 問題 用YouTubePlayerView(https://github.com/PierfrancescoSoffritti/android-youtube-player)在部分Android5.0手機上初始化的時候出現手機奔潰,錯誤提示如下 關鍵日志如下: Error inflating class com.pierfrancescosoffritti.androidyoutubeplayer.core.player.v…

重慶大學計算機課程設計,計算機組成原理課程設計報告重慶大學.doc

計算機組成原理課程設計報告重慶大學計算機組成原理課程設計報告題目&#xff1a;微程序設計專業&#xff1a;計算機科學與技術班級&#xff1a;指導老師&#xff1a;目錄一、摘要二、設計要求三、微程序控制器原理四、總體設計五、測試六、結論七、參考文獻目錄八、 個人總結重…