Flutter&Dart

哥哥带你Flutter实战五步走,第一步,网络配置

2019-09-28  本文已影响0人  哥哥是欧巴Vitory

一,配置headers:

const httpHeaders = {

'Accept':'application/json, text/plain, */*',

  'Accept-Encoding':'gzip, deflate, br',

  'Accept-Language':'zh-CN,zh;q=0.9',

  'Connection':'keep-alive',

  'Content-Length':'2',

  'Content-Type':'application/json',

  'Cookie':'_ga=GA1.2.64752568.1562546452; GCID=d7bd452-982783a-915ae42-995fcb2; GCESS=BAUEAAAAAAoEAAAAAAMEL5EiXQYEden3RQQEAC8NAAsCBAAHBAK78CYJAQEMAQECBC.RIl0BBCFKEgAIAQM-; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1562546485,1562664590,1562664606,1562911778; _gid=GA1.2.1359324536.1562911778; _gat=1; SERVERID=1fa1f330efedec1559b3abbcb6e30f50|1562911819|1562911777; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1562911820',

  'Host':'time.geekbang.org',

  'Origin':'https://time.geekbang.org',

  'Referer':'https://time.geekbang.org/',

  'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'

};

二,配置Service_url

const serviceUrl ='https://wxmini.baixingliangfan.cn/baixing/';

const servicePath = {

'homePageContent':serviceUrl +'wxmini/homePageContent',//商店首页信息

  'homePageBelowConten':serviceUrl +'wxmini/homePageBelowConten',//首页火爆专区

  'getCategory': serviceUrl+'wxmini/getCategory', //商品类别信息

  'getMallGoods': serviceUrl+'wxmini/getMallGoods', //商品分类的商品列表

  'getGoodDetailById': serviceUrl+'wxmini/getGoodDetailById', //商品详情页面

};

三,获取主题内容,设置formData是可选参数

//获取首页主题内容 设置formData是可选参数

Futurerequest(url, {formData})async {

try {

print('开始获取数据..........');

    Response response;

    Dio dio =Dio();

    dio.options.contentType =

ContentType.parse('application/x-www-form-urlencoded');

    if (formData ==null) {

response =await dio.post(servicePath[url]);

    }else {

response =await dio.post(servicePath[url], data: formData);

    }

if (response.statusCode ==200) {

return response.data;

    }else {

throw Exception('后端接口出现异常');

    }

}catch (error) {

return print('ERROR:=======================>${error}');

  }

}

上一篇下一篇

猜你喜欢

热点阅读