flutter前端

Flutter 之网络请求Dio, FormData, 表单网络

2019-12-03  本文已影响0人  壹点微尘

网络请求, 先想到的是dart官方维护的http库. 由于我们项目组网络请求都采用的表单结构, http貌似不支持表单格式的网络请求; 后来查看dio库, 发现支持FormData, 完美解决!

dio是一个强大的Dart Http请求库,支持Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载、超时、自定义适配器等, 被他强大的功能所吸引, 所以项目决定使用. github地址

官方表单网络请求示例:

FormData formData = FormData.fromMap({
    "name": "wendux",
    "age": 25,
    "file": await MultipartFile.fromFile("./text.txt",filename: "upload.txt"),
    "files": [
      await MultipartFile.fromFile("./text1.txt", filename: "text1.txt"),
      await MultipartFile.fromFile("./text2.txt", filename: "text2.txt"),
    ]
 });
response = await dio.post("/info", data: formData);

比葫芦画瓢, 尝试下

  /*获取网络请求*/
  _getHttpRequest() async {
    var url = 'https://t.duodian.api.cheng1hu.com/v1/index/recommendDoctor';

    /*由于服务器是表单结构*/
    FormData formData = FormData.from({
      "channel": "ios",
      "version": '1.0.0',
      'sign': 'test',
    });

    var dio = new Dio();
    var response = await dio.post(url, data:formData);
    String res = response.data.toString();

    print(res);
    print(res.runtimeType); // String

    var resJson = convert.jsonDecode(res); // 字符串反序列化为Map
    print(resJson); // 此时\u5317\u4eac\u8317\u89c6\u5149的数据也被解码成了中文.
    print(resJson.runtimeType); //  _InternalLinkedHashMap<String, dynamic>
    
  }

打印结果:
flutter: {"status":1,"data":[{"img":"https:\/\/t.duodian.api.cheng1hu.com\/attachment\/doctor\/7086905c8ca423a57596f348dbaff729.jpg","label":"\u4e3b\u4efb\u533b\u5e08","name":"\u5546\u5fae","depart":"\u5987\u4ea7\u79d1","hospital":"\u4e2d\u56fd\u4eba\u6c11\u89e3\u653e\u519b\u603b\u533b\u9662\u7b2c\u516d\u533b\u5b66\u4e2d\u5fc3(\u539f\u6d77\u519b\u603b\u533b\u9662)","docId":45028,"like":"\u8f85\u52a9\u751f\u6b96\u6280\u672f(\u5c24\u5176\u64c5\u957f\u5fae\u523a\u6fc0\u548c\u81ea\u7136\u5468\u671f)\u3001\u4e0d\u5b55\u75c7\u53ca\u4e60\u60ef\u6027\u6d41\u4ea7\u7684\u8bca\u6cbb\u4ee5\u53ca\u9057\u4f20\u54a8\u8be2","count":1271},{"img":"https:\/\/t.duodian.api.cheng1hu.com\/attachment\/doctor\/2019\/10\/10\/16\/20191010162255_5d9eea5fbb5de_246313.jpeg","label":"\u4e3b\u4efb\u533b\u5e08","name":"\u5468\u8dc3\u534e","depart":"\u773c\u79d1","hospital":"\u5317\u4eac\u8317\u89c6\u5149\u773c\u79d1","docId":14301,"like":"\u98de\u79d2\uff0c\u5168\u98de\u79d2\uff0c\u6fc0\u5149\u8fd1\u89c6\u773c\u624b\u672f\uff<…>
flutter: String
flutter: {status: 1, data: [{img: https://t.duodian.api.cheng1hu.com/attachment/doctor/7086905c8ca423a57596f348dbaff729.jpg, label: 主任医师, name: 商微, depart: 妇产科, hospital: 中国人民解放军总医院第六医学中心(原海军总医院), docId: 45028, like: 辅助生殖技术(尤其擅长微刺激和自然周期)、不孕症及习惯性流产的诊治以及遗传咨询, count: 1271}, {img: https://t.duodian.api.cheng1hu.com/attachment/doctor/2019/10/10/16/20191010162255_5d9eea5fbb5de_246313.jpeg, label: 主任医师, name: 周跃华, depart: 眼科, hospital: 北京茗视光眼科, docId: 14301, like: 飞秒,全飞秒,激光近视眼手术,圆锥角膜,近视,远视,散光,角膜交联手术,, count: 758}, {img: https://t.duodian.api.cheng1hu.com/upload/cy/2019/09/19/19/20190919192759_5d83663f26f0f_707496.png, label: 主任医师, name: 李媛, depart: 生殖中心, hospital: 北京朝阳医院, docId: 2604, like: 妇科内分泌性疾病和各种不孕症<…>
flutter: _InternalLinkedHashMap<String, dynamic>
上一篇下一篇

猜你喜欢

热点阅读