【问题标题】:Dart Http Client returns truncated response, truncation varies slightly when I print to consoleDart Http Client 返回截断的响应,当我打印到控制台时截断略有不同
【发布时间】:2019-04-15 12:16:21
【问题描述】:

我正在尝试读取 JSON 响应并使用 Flutter 的 FutureBuilder 构建 UI,但我无法在客户端上获取整个 JSON 响应。当我尝试在两个不同的打印语句中打印响应时,它们之间的内容略有不同。请注意以下控制台打印输出对于 My Posts API ResponseMy Posts Json Response String 有何不同。

有人可以解释这种行为以及如何在客户端接收到完整的 JSON 数组之前实现正确的侦听吗?

预期的 API 响应 -

[{"_id":"5bd11c9b8a9fc0a744d1bebc","postId":1,"postUserId":1,"postDescription":"First Post with 2 Photos","postLongDescription":"First Post with 2 Photos","postDateTime":"2018-07-27T10:50:42.389Z","postLocationId":1,"postHasMedia":true,"postActive":true,"postLikesCount":3,"postCommentsCount":1},{"_id":"5bd11c9b8a9fc0a744d1bebd","postId":2,"postUserId":2,"postDescription":"Second Post with 1 Video","postLongDescription":"Second Post with 1 Video","postDateTime":"2018-07-27T11:02:00.389Z","postLocationId":2,"postHasMedia":true,"postActive":true,"postLikesCount":12,"postCommentsCount":2},{"_id":"5bd11c9b8a9fc0a744d1bebe","postId":3,"postUserId":2,"postDescription":"Third Post with No Video","postLongDescription":"Third Post with No Video","postDateTime":"2018-07-27T11:12:34.389Z","postLocationId":3,"postHasMedia":false,"postActive":true,"postLikesCount":9,"postCommentsCount":0},{"_id":"5bd11c9b8a9fc0a744d1bebf","postId":4,"postUserId":3,"postDescription":"Fourth Post with 1 Photo but Disabled","postLongDescription":"Fourth Post with 1 Photo but Disabled","postDateTime":"2018-07-27T11:12:34.389Z","postLocationId":2,"postHasMedia":true,"postActive":false,"postLikesCount":4,"postCommentsCount":0}]

Flutter Bloc 代码 -

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:MyApp/models/post_model.dart';    
import 'package:http/http.dart' as http;    

enum storyTypes {
    timeline,
    myposts
}

class PostsBloc {

Future<PostModel> getPosts(storyTypes storyType) async {
    if (storyType == storyTypes.myposts) {
        final String url = "http://127.0.0.1:8081/posts/all";
        return await http.get(url).then((getMyPostsApiResponse) {
            if (getMyPostsApiResponse.statusCode != 200) {
            throw Exception("Error with http over network");
            }
            else {
            if (getMyPostsApiResponse.statusCode == 200) {
                print('My Posts API Response - ' + getMyPostsApiResponse.body);                    
                print('My Posts Json Response String - ' + json.decode(getMyPostsApiResponse.body).toString());
                return PostModel.fromJson(json.decode(getMyPostsApiResponse.body));
            }
            else {
                throw Exception('Failed to load post');
            }
            }
        });
    }
}

Flutter 控制台输出 -

I/flutter (32316): My Posts API Response - [{"_id":"5bd11c9b8a9fc0a744d1bebc","postId":1,"postUserId":1,"postDescription":"First Post with 2 Photos","postLongDescription":"First Post with 2 Photos","postDateTime":"2018-07-27T10:50:42.389Z","postLocationId":1,"postHasMedia":true,"postActive":true,"postLikesCount":3,"postCommentsCount":1},{"_id":"5bd11c9b8a9fc0a744d1bebd","postId":2,"postUserId":2,"postDescription":"Second Post with 1 Video","postLongDescription":"Second Post with 1 Video","postDateTime":"2018-07-27T11:02:00.389Z","postLocationId":2,"postHasMedia":true,"postActive":true,"postLikesCount":12,"postCommentsCount":2},{"_id":"5bd11c9b8a9fc0a744d1bebe","postId":3,"postUserId":2,"postDescription":"Third Post with No Video","postLongDescription":"Third Post with No Video","postDateTime":"2018-07-27T11:12:34.389Z","postLocationId":3,"postHasMedia":false,"postActive":true,"postLikesCount":9,"postCommentsCount":0},{"_id":"5bd11c9b8a9fc0a744d1bebf","postId":4,"postUserId":3,"postDescription":"Fourth Post with 1 Photo but Dis    
I/flutter (32316): My Posts Json Response String - [{_id: 5bd11c9b8a9fc0a744d1bebc, postId: 1, postUserId: 1, postDescription: First Post with 2 Photos, postLongDescription: First Post with 2 Photos, postDateTime: 2018-07-27T10:50:42.389Z, postLocationId: 1, postHasMedia: true, postActive: true, postLikesCount: 3, postCommentsCount: 1}, {_id: 5bd11c9b8a9fc0a744d1bebd, postId: 2, postUserId: 2, postDescription: Second Post with 1 Video, postLongDescription: Second Post with 1 Video, postDateTime: 2018-07-27T11:02:00.389Z, postLocationId: 2, postHasMedia: true, postActive: true, postLikesCount: 12, postCommentsCount: 2}, {_id: 5bd11c9b8a9fc0a744d1bebe, postId: 3, postUserId: 2, postDescription: Third Post with No Video, postLongDescription: Third Post with No Video, postDateTime: 2018-07-27T11:12:34.389Z, postLocationId: 3, postHasMedia: false, postActive: true, postLikesCount: 9, postCommentsCount: 0}, {_id: 5bd11c9b8a9fc0a744d1bebf, postId: 4, postUserId: 3, postDescription: Fourth Post with 1 Photo but Disabled, postLongDescr

【问题讨论】:

    标签: json http dart flutter


    【解决方案1】:

    看起来您在 Android 上尝试打印一个长字符串。引用documentation about debugging

    Dart print() 函数输出到系统控制台,你可以 使用颤振日志查看(基本上是 adb 的包装器) 日志猫)。

    如果你一次输出太多,那么 Android 有时会丢弃一些 日志行。为了避免这种情况,你可以使用来自 Flutter 的 debugPrint() 基础图书馆。这是 print 的包装,可以限制 输出到避免被 Android 内核丢弃的级别。

    所以您可以尝试改用debugPrint

    导入 Flutter 包以使用 debugPrint - import 'package:flutter/foundation.dart';

    【讨论】:

    • 感谢@ringil,我从文档中学到了一些关于 debugPrint 和 Flutter 中的一般调试的知识,但是当我使用 debugPrintdebugPrintSynchronously 方法打印时,截断是总是存在但是谢谢! :)
    • 请注意,如果jsonDecode 成功,则它非常不太可能在此之前被截断,否则您最终会出现解码错误。截断仍然可能发生在打印的某个地方。
    【解决方案2】:

    这只是控制台在这么多字符后截断。

    请注意,您正在打印两种不同的东西:

    getMyPostsApiResponse.body是从服务器接收到的json。

    json.decode(getMyPostsApiResponse.body).toString()是解码后的List的toString(不包括引号,这就是字符串长度不同的原因)

    【讨论】:

    • 您在缺少的引号上有一个点,因此响应的长度由于 JSON 解码而略有不同,但它不能解释响应的截断。无论如何谢谢:)
    猜你喜欢
    • 2015-12-13
    • 2013-03-02
    • 2019-04-24
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多