欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter網(wǎng)絡(luò)請(qǐng)求的3種簡(jiǎn)單實(shí)現(xiàn)方法

 更新時(shí)間:2019年04月04日 08:36:55   作者:Silence_Zhou  
這篇文章主要給大家介紹了給大家Flutter網(wǎng)絡(luò)請(qǐng)求的3種簡(jiǎn)單實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

概述:

App幾乎都離不開(kāi)與服務(wù)器的交互,本文主要講解了flutter網(wǎng)絡(luò)請(qǐng)求三種方式 flutter自帶的HttpClient、 第三方庫(kù)http 和 第三方庫(kù)Dio  的簡(jiǎn)單實(shí)現(xiàn) GET 和 POST請(qǐng)求,本文是筆者學(xué)習(xí)Flutter網(wǎng)絡(luò)模塊知識(shí)總結(jié),若有問(wèn)題還望不膩賜教。

一.系統(tǒng)自帶HttpClient

1.使用中溫馨提示

1.1.導(dǎo)入庫(kù)

import 'dart:io'; // 網(wǎng)絡(luò)請(qǐng)求
import 'dart:convert'; // 數(shù)據(jù)解析

1.2.Uri的多種初始化方式

// 方法1
Uri uri = Uri(scheme: 'https', host: 'app.xxx.com', path: homeNoviceListUrl);
// 方法2
Uri uri = Uri.https('app.xxx.com', homeNoviceListUrl);
// uri方法3
Uri uri = Uri.parse(baseUrl + homeNoviceListUrl);

2.簡(jiǎn)單使用

2.1.GET請(qǐng)求

// 1.1 HttpClient - get 

void loadData_sys_get() async {
print('------loadData_sys_get--------');

var httpClient = new HttpClient();
var params = Map<String, String>();

// uri方法1
Uri uri =
 Uri(scheme: 'https', host: 'app.xxx.com', path: homeNoviceListUrl);

// uri方法2
// Uri uri = Uri.https(
// 'app.xxx.com', homeNoviceListUrl);

// uri方法3
// Uri uri = Uri.parse(baseUrl + homeNoviceListUrl);

var request = await httpClient.getUrl(uri);

var headers = Map<String, String>();
headers['loginSource'] = 'IOS';
headers['useVersion'] = '3.1.0';
headers['isEncoded'] = '1';
headers['bundleId'] = 'com.xxx.xxx';

request.headers.add("loginSource", "IOS");
request.headers.add("useVersion", "3.1.0");
request.headers.add("isEncoded", "1");
request.headers.add("bundleId", "com.xxx.xxx");

var response = await request.close();
var responseBody = await response.transform(Utf8Decoder()).join();

if (response.statusCode == HttpStatus.ok) {
 print('請(qǐng)求頭:${response.headers}');

 print('111請(qǐng)求成功代發(fā)數(shù)據(jù)為:\n $responseBody');
 print('--------------');
 Map data = jsonDecode(responseBody);
 print('222請(qǐng)求成功代發(fā)數(shù)據(jù)為:\n $data');
} else {
 print('\n\n\n11111==請(qǐng)求失敗${response.statusCode}');
}
}

2.2.POST請(qǐng)求

注意點(diǎn):請(qǐng)求參數(shù)需要編碼后放在request中

void loadData_sys_post() async {
print('------loadData_sys_post--------');

HttpClient httpClient = new HttpClient();

// queryParameters get請(qǐng)求的查詢(xún)參數(shù)(適用于get請(qǐng)求???是嗎???)
// Uri uri = Uri(
// scheme: "https", host: "app.xxx.com", path: homeRegularListUrl);
// HttpClientRequest request = await httpClient.postUrl(uri);

var url = baseUrl + homeRegularListUrl;
HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));

// 設(shè)置請(qǐng)求頭
request.headers.set("loginSource", "IOS");
request.headers.set("useVersion", "3.1.0");
request.headers.set("isEncoded", "1");
request.headers.set("bundleId", "com.xxx.xxx");
// Content-Type大小寫(xiě)都o(jì)k
request.headers.set('content-type', 'application/json');

/// 添加請(qǐng)求體
/// https://stackoverflow.com/questions/50278258/http-post-with-json-on-body-flutter-dart/50295533
Map jsonMap = {'currentPage': '1'};
request.add(utf8.encode(json.encode(jsonMap)));

HttpClientResponse response = await request.close();
String responseBody = await response.transform(utf8.decoder).join();
if (response.statusCode == HttpStatus.ok) {
 print('請(qǐng)求成功');
 print(response.headers);
 print(responseBody);
}
}

二.請(qǐng)求第三方庫(kù) http

1.使用中溫馨提示

1.1.添加依賴(lài)

dependencies:
 http: ^0.12.0 #latest version

1.2.導(dǎo)入庫(kù)

import 'package:http/http.dart' as http; //導(dǎo)入前需要配置

2.簡(jiǎn)單使用

2.1. GET請(qǐng)求

2.2.1. http - get1

 void loadData_http_get() async {
 print('------loadData_http_get--------');

 var client = http.Client();

 var uri = Uri.parse(baseUrl + homeNoviceListUrl);

 http.Response response = await client.get(uri);

 if (response.statusCode == HttpStatus.ok) {
 print(response.body);
 } else {
 print('請(qǐng)求失敗 code 碼為${response.statusCode}');
 }
 }

2.2. http - get簡(jiǎn)便方法(鏈?zhǔn)骄幊蹋?br />

void loadData_http_get_convenience() async {
 print('------簡(jiǎn)便方法loadData_http_get_convenience--------');

 var uri = Uri.parse(baseUrl + homeNoviceListUrl);

 http.Client().get(uri).then((http.Response response) {
 if (response.statusCode == HttpStatus.ok) {
 print(response.body);
 } else {
 print('請(qǐng)求失敗 code 碼為${response.statusCode}');
 }
 });
 }

2.2. POST請(qǐng)求

2.2.1.  http - post

 void loadData_http_post() async {
 print('------ loadData_http_post --------');

 var headers = Map<String, String>();
 headers["loginSource"] = "IOS";
 headers["useVersion"] = "3.1.0";
 headers["isEncoded"] = "1";
 headers["bundleId"] = "com.xxx.xxx";
 headers["loginSource"] = "IOS";
 headers["Content\-Type"] = "application/json";

 Map params = {'currentPage': '1'};
 // 嵌套兩層都可以,但是具體哪個(gè)好還有待確認(rèn)????
 var jsonParams = utf8.encode(json.encode(params));
 // var jsonParams = json.encode(params);

 var httpClient = http.Client();

 var uri = Uri.parse(baseUrl + homeNoviceListUrl);

 http.Response response =
 await httpClient.post(uri, body: jsonParams, headers: headers);

 if (response.statusCode == HttpStatus.ok) {
 print(response.body);
 } else {
 print('請(qǐng)求失敗 code 碼���${response.statusCode}');
 }
 }

2.2.2. http - Post簡(jiǎn)便方法(鏈?zhǔn)骄幊蹋?/p>

 void loadData_http_post_convenience() async {
 print('------ loadData_http_post --------');

 var headers = Map<String, String>();
 headers["loginSource"] = "IOS";
 headers["useVersion"] = "3.1.0";
 headers["isEncoded"] = "1";
 headers["bundleId"] = "com.xxx.xxx";
 headers["loginSource"] = "IOS";
 headers["Content\-Type"] = "application/json";

 Map params = {'currentPage': '1'};
 // 嵌套兩層都可以,但是具體哪個(gè)好還有待確認(rèn)????
 var jsonParams = utf8.encode(json.encode(params));
 // var jsonParams = json.encode(params);

 var httpClient = http.Client();

 var url = baseUrl + homeRegularListUrl;

 httpClient.post(url, body: jsonParams, headers: headers).then((response) {
 print("Response status: ${response.statusCode}");
 print("Response body: ${response.body}");
 }).whenComplete(httpClient.close);
 }

三.請(qǐng)求第三方庫(kù) Dio

1.使用中溫馨提示

1.1.添加依賴(lài)

dependencies:
 dio: ^2.0.11 #latest version

1.2.導(dǎo)入庫(kù)

import 'package:dio/dio.dart';

2.簡(jiǎn)單使用

2.1. GET請(qǐng)求

注意:Dio的get請(qǐng)求(baseUrl都是在dio.option.baseUrl設(shè)置的) 請(qǐng)求頭可以在dio.option上設(shè)置,也可以在新建的option上設(shè)置,新建option是可選的

void loadData_dio_get() async {
 var headers = Map<String, String>();
 headers['loginSource'] = 'IOS';
 headers['useVersion'] = '3.1.0';
 headers['isEncoded'] = '1';
 headers['bundleId'] = 'com.xxx.xxx';
 headers['Content-Type'] = 'application/json';

 Dio dio = Dio();
 dio.options.headers.addAll(headers);
 dio.options.baseUrl = baseUrl;

 Response response = await dio.get(homeNoviceListUrl);

 if (response.statusCode == HttpStatus.ok) {
 print(response.headers);
 print(response.data);
 }
 }

2.2. POST請(qǐng)求

注意:

dio.options.method設(shè)置是無(wú)效
Dio dio = Dio();
dio.options.method = 'post';

辦法:
新建一個(gè)Options對(duì)象,然后在發(fā)起請(qǐng)求的時(shí)候進(jìn)行設(shè)置:
Options option = Options(method:'post');
Response response = await dio.request(homeRegularListUrl,data:{"currentPage": "1"}, options: option);

2.2.1. dio - 方式一(baseUrl都是在dio.option.baseUrl設(shè)置的)

注意:直接在 dio.options設(shè)置除methods以外的 請(qǐng)求頭參數(shù)

void loadData_dio_dioOfOptionsSetting() async {
 debugPrint(
 ' \n post請(qǐng)求 ======================= 開(kāi)始請(qǐng)求 =======================\n');
 var headers = Map<String, String>();
 headers['loginSource'] = 'IOS';
 headers['useVersion'] = '3.1.0';
 headers['isEncoded'] = '1';
 headers['bundleId'] = 'com.xxx.xxx';
 headers['Content-Type'] = 'application/json';

 Dio dio = Dio();
 dio.options.baseUrl = baseUrl;
 dio.options.connectTimeout = 60000;
 dio.options.receiveTimeout = 60000;
 dio.options.headers.addAll(headers);
 dio.options.method = 'post';

 Options option = Options(method: 'post');
 // Response response = await dio.request(homeRegularListUrl,
 // data: {"currentPage": "1"}, options: option);

 Response response = await dio.post(homeRegularListUrl,
 data: {"currentPage": "1"}, options: option);

 if (response.statusCode == HttpStatus.ok) {
 debugPrint('請(qǐng)求參數(shù): ${response.request.queryParameters}');
 debugPrint(
  '-------------------請(qǐng)求成功,請(qǐng)求結(jié)果如下:-----------------\n \n===請(qǐng)求求url: ${response.request.uri.toString()} \n \n===請(qǐng)求 ���: \n${response.headers} \n \n===請(qǐng)求結(jié)果: \n${response.data}\n');
 debugPrint('-------------------請(qǐng)求成功,請(qǐng)求結(jié)果打印完畢----------------');
 } else {
 print('請(qǐng)求失敗');
 }
 }

2.2.2. dio - 方式二(baseUrl都是在dio.option.baseUrl設(shè)置的)

注意:在新建的option上設(shè)置請(qǐng)求頭參數(shù)

void loadData_dio_newOptionSetting() async {
 debugPrint(' \n======================= 開(kāi)始請(qǐng)求 =======================\n');
 var headers = Map<String, String>();
 headers['loginSource'] = 'IOS';
 headers['useVersion'] = '3.1.0';
 headers['isEncoded'] = '1';
 headers['bundleId'] = 'com.xxx.xxx';
 headers['Content-Type'] = 'application/json';

 Options option = Options(method: 'post');
 option.connectTimeout = 60000;
 option.receiveTimeout = 60000;
 option.headers.addAll(headers);

 Dio dio = Dio();
 dio.options.baseUrl = baseUrl;

 Response response = await dio.post(homeRegularListUrl,
 data: {"currentPage": 1}, options: option);
 // Response response = await dio.request(homeRegularListUrl,
 // data: {"currentPage": 1}, options: option);

 if (response.statusCode == HttpStatus.ok) {
 debugPrint('請(qǐng)求參數(shù): ${response.request.queryParameters}');
 debugPrint(
  '-------------------請(qǐng)求成功,請(qǐng)求結(jié)果如下:-----------------\n \n===請(qǐng)求url: ${response.request.uri.toString()} \n \n===請(qǐng)求 頭: \n${response.headers} \n \n===請(qǐng)求結(jié)果: \n${response.data}\n');
 debugPrint('-------------------請(qǐng)求成功,請(qǐng)求結(jié)果打印完畢----------------');
 } else {
 print('請(qǐng)求失敗');
 }
 }

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論