gpt4 book ai didi

json - Dart Http 客户端返回截断的响应,当我打印到控制台时截断略有不同

转载 作者:IT王子 更新时间:2023-10-29 07:07:40 27 4
gpt4 key购买 nike

我正在尝试读取 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

最佳答案

看起来你在 Android 上试图打印一个长字符串。引用自documentation about debugging :

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.

因此您可以尝试使用 debugPrint

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

关于json - Dart Http 客户端返回截断的响应,当我打印到控制台时截断略有不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53267877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com