Flutter基础篇06--网络请求,自定义请求头

这里我要抓取这个接口数据,并在页面展示出来请求的数据

这是极客时间网址首页的一个接口

Flutter基础篇06--网络请求,自定义请求头_第1张图片

效果如下:


Flutter基础篇06--网络请求,自定义请求头_第2张图片

步骤:

1、项目集成Dio库

Flutter基础篇06--网络请求,自定义请求头_第3张图片

然后到需要发请求的地方引入头文件

import 'package:dio/dio.dart';

2、画界面

String resStr = '还没有请求数据';

@override
Widget build(BuildContext context) {
  return Container(
    child: Scaffold(
      appBar: AppBar(title: Text('网络数据请求'),),
      body: SingleChildScrollView(
        child: Column(
          children: [
            RaisedButton(
              onPressed: _btnClicked,
              child: Text('点击请求'),
            ),
            Text(resStr)
          ],
        ),
      ),
    ),
  );
}

3、 创建请求头

Flutter基础篇06--网络请求,自定义请求头_第4张图片
请求头怎么来的呢?

在如下位置


Flutter基础篇06--网络请求,自定义请求头_第5张图片

4、发请求,显示数据

void _btnClicked(){

  postMethod().then((value){
    setState(() {
      resStr = value['data'].toString();
    });
  });
}

Future postMethod() async {
  
  try{
    Response response;
    Dio dio = Dio();
    dio.options.headers = httpHeaders;
    response = await dio.post('https://time.geekbang.org/serv/v1/column/newAll');
    print(response.data);
    return response.data;
  }catch(e){
    print(e);
  }
}

最后给出完整代码

home_page.dart
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import '../config/http_headers.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State {
  
  String resStr = '还没有请求数据';
  
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(title: Text('网络数据请求'),),
        body: SingleChildScrollView(
          child: Column(
            children: [
              RaisedButton(
                onPressed: _btnClicked,
                child: Text('点击请求'),
              ),
              Text(resStr)
            ],
          ),
        ),
      ),
    );
  }

  void _btnClicked(){

    postMethod().then((value){
      setState(() {
        resStr = value['data'].toString();
      });
    });
  }

  Future postMethod() async {
    
    try{
      Response response;
      Dio dio = Dio();
      dio.options.headers = httpHeaders;
      response = await dio.post('https://time.geekbang.org/serv/v1/column/newAll');
      print(response.data);
      return response.data;
    }catch(e){
      print(e);
    }
  }
}

http_headers.dart

const httpHeaders={
  'Accept': 'application/json, text/plain, */*',
  'Accept-Encoding': 'gzip, deflate, br',
  'Accept-Language': 'zh-CN,zh;q=0.9',
  'Connection': 'keep-alive',
  'Content-Type': 'application/json',
  'Cookie': '_ga=GA1.2.676402787.1548321037; GCID=9d149c5-11cb3b3-80ad198-04b551d; _gid=GA1.2.359074521.1550799897; _gat=1; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1550106367,1550115714,1550123110,1550799897; SERVERID=1fa1f330efedec1559b3abbcb6e30f50|1550799909|1550799898; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1550799907',
  'Host': 'time.geekbang.org',
  'Origin': 'https://time.geekbang.org',
  'Referer': 'https://time.geekbang.org/',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
};

你可能感兴趣的:(Flutter基础篇06--网络请求,自定义请求头)