use*_*610 3 android api-design httprequest dart dart-pub
我意识到目前至少有三个“官方”Dart 库允许我执行 HTTP 请求。更重要的是,其中三个库(dart:io(类 HttpClient)、package:http 和 dart:html)都有不同的、不兼容的 API。
截至今天,package:html 不提供此功能,但在其 GitHub 页面上,我发现它旨在与 dart:html 100% API 兼容,因此这些方法最终将添加到那里。
哪个包提供了最未来证明和平台独立的 API 来在 Dart 中发出 HTTP 请求?
是包:http吗?
import 'package:http/http.dart' as http;
var url = "http://example.com";
http.get(url)
.then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
});
Run Code Online (Sandbox Code Playgroud)
是 dart:html/package:html 吗?
import 'dart:html';
HttpRequest.request('/example.json')
.then((response) {
print("Response status: ${response.status}");
print("Response body: ${response.response}");
});
Run Code Online (Sandbox Code Playgroud)
还是飞镖:io?
import 'dart:io';
var client = new HttpClient();
client.getUrl(Uri.parse("http://www.example.com/"))
.then((HttpClientRequest request) {
// Optionally set up headers...
// Optionally write to the request object...
// Then call close.
...
return request.close();
})
.then((HttpClientResponse response) {
print("Response status: ${response.statusCode}");
print("Response body:");
response.transform(UTF8.decoder).listen((contents) {
print(contents);
});
});
Run Code Online (Sandbox Code Playgroud)
假设我也想涵盖 Android。这也增加了 package:sky 的组合(https://github.com/domokit/sky_sdk/)。我承认这不是“官方”谷歌图书馆。
import 'package:sky/framework/net/fetch.dart';
Response response = await fetch('http://example.com');
print(response.bodyAsString());
Run Code Online (Sandbox Code Playgroud)
什么是(将是)常规产品是https://www.youtube.com/watch?v=t8xdEO8LyL8。我想知道他们的HTTP 请求故事会是什么。有些东西告诉我这将是另一种与我们迄今为止所见的不同的野兽。
该html
包是一个 HTML 解析器,它允许与 HTML 服务器端一起工作。我不希望它获得一些 HttpRequest 功能。
该http
包旨在为客户端和服务器 Dart 代码提供统一的 API。中的 APIdart:html
只是浏览器提供的 API 的包装器。中的 HttpRequest API 是在dart:io
没有浏览器限制的情况下构建的,因此偏离了dart:html
. package:http
提供了一个统一的 API,该 API 委托dart:html
何时在浏览器中dart:io
运行和何时在服务器上运行。
我认为package:http
是面向未来和跨平台的,应该非常适合您的要求。
归档时间: |
|
查看次数: |
916 次 |
最近记录: |