dio icon indicating copy to clipboard operation
dio copied to clipboard

dio为什么当返回json时就报错?

Open cedear opened this issue 8 years ago • 3 comments

Steps to Reproduce

import 'package:dio/dio.dart';

Future main() async { Dio dio = new Dio(); Response response = await dio.get("https://www.mocky.io/v2/5185415ba171ea3a00704eed"); print(response.data); }

这个url是我mock的,数据在我打断点的时候数据也会返回,但是会报错,当我请求www.baidu.com的时候,会正确的返回数据并打印。原因就在于我请求的数据返回的json格式,而我请求百度的时候返回的是百度网页的html信息,请问还需要配置其他什么信息吗?

Logs

Unhandled exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' #0 Dio._assureResponse (package:dio/src/dio.dart:612:9) #1 Dio.resolve. (package:dio/src/dio.dart:121:14) #2 _RootZone.runUnary (dart:async/zone.dart:1379:54) #3 _FutureListener.handleValue (dart:async/future_impl.dart:129:18) #4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45) #5 Future._propagateToListeners (dart:async/future_impl.dart:671:32) #6 Future._completeWithValue (dart:async/future_impl.dart:486:5) #7 Future._asyncComplete. (dart:async/future_impl.dart:516:7) #8 _microtaskLoop (dart:async/schedule_microtask.dart:41:21) #9 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5) #10 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:391:30) #11 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5) #12 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

cedear avatar Oct 18 '18 07:10 cedear

如果返回的content-type为json,则dio会自动返回内容转为Map或List.

wendux avatar Oct 18 '18 07:10 wendux

如果返回的content-type为json,则dio会自动返回内容转为Map或List. 请问那怎么才能获取到返回的json字符串呢?或者说如何规避这个报错?

cedear avatar Oct 18 '18 08:10 cedear

我在换成print(response.data.toString())之后还是会报这个错误

cedear avatar Oct 18 '18 08:10 cedear