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 {
}
}
@override
Widget 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.