}
以上代码中httpClient.getUrl(uri)方法即是根据uri发起get请求,post请求用法和get请求只是调用方法不同,包括参数设置等其他用法都一样。请求抓包结果如下:
- get请求
- post请求
请求成功之后以上代码输出结果如下:
I/flutter (19183): 请求成功
I/flutter (19183): responseBody={“resultcode”:“200”,“reason”:“查询成功”,“result”:{“sk”:{“temp”:“8”,“wind_direction”:“西南风”,“wind_strength”:“2级”,“humidity”:“51%”,“time”:“21:54”},“today”:{“temperature”:“1℃15℃",“weather”:“晴”,“weather_id”:{“fa”:“00”,“fb”:“00”},“wind”:“南风微风”,“week”:“星期五”,“city”:“北京”,“date_y”:“2019年03月01日”,“dressing_index”:“较冷”,“dressing_advice”:“建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。”,“uv_index”:“中等”,“comfort_index”:“”,“wash_index”:“较适宜”,“travel_index”:“较不宜”,“exercise_index”:“较不宜”,“drying_index”:“”},“future”:[{“temperature”:"1℃15℃”,“weather”:“晴”,“weather_id”:{“fa”:“00”,“fb”:“00”},“wind”:“南风微风”,“week”:“星期五”,“date”:“20190301”},{“temperature”:“3℃15℃",“weather”:“霾”,“weather_id”:{“fa”:“53”,“fb”:“53”},“wind”:“西南风微风”,“week”:“星期六”,“date”:“20190302”},{“temperature”:"3℃15℃”,“weather”:“霾转晴”,“weather_id”:{“fa”:“53”,“fb”:“00”},“wind”:“西南风微风”,"
由此可以看出结果输出不完整,部分字符串被丢弃了,前段时间有个同学留言问过这个问题,在这里说明一下。Flutter官方文档有这个问题的说明:
The Dart print() function outputs to the system console, which you can view using flutter logs (which is basically a wrapper around adb logcat).
If you output too much at once, then Android sometimes discards some log lines. To avoid this, you can use debugPrint(), from Flutter’s foundation library. This is a wrapper around print which throttles the output to a level that avoids being dropped by Android’s kernel.
具体地址在:
https://flutter.dev/docs/testing/debugging#print-and-debugprint-with-flutter-logs
官方建议我们输出日志过多的时候使用debugPrint()函数,作者使用debugPrint()函数之后发现依然是输出不完整,查看debugPrint()函数的具体代码实现后发现以上输出的内容是一行的内容,一行内容过多的情况下debugPrint()函数依然无法完整进行输出,最后我采用了一个技巧,对以上返回的responseBody结果进行了简单处理,根据特殊字符串进行字符串替换,加入换行符,代码如下:
var responseBody = await response.transform(utf8.decoder).join();
//‘{’符号前加个换行符
responseBody = responseBody.replaceAll(“{”, “\n{”);
debugPrint(‘responseBody=$responseBody’);
于是输出了完整的请求返回结果:
I/flutter (19183): 请求成功
I/flutter (19183): responseBody=
I/flutter (19183): {“resultcode”:“200”,“reason”:“successed!”,“result”:
I/flutter (19183): {“sk”:
I/flutter (19183): {“temp”:“8”,“wind_direction”:“西南风”,“wind_strength”:“2级”,“humidity”:“51%”,“time”:“2