gpt4 book ai didi

http - Flutter http post 出错但在 postman 中有效

转载 作者:IT王子 更新时间:2023-10-29 06:36:59 25 4
gpt4 key购买 nike

我正在尝试向响应如下所示的网址发出发布请求

{"status":1,"msg":"Success"}

下面是我的代码

void signOut(String userId, String loginType, BuildContext context) async {
print(userId + "\nFargo" + loginType);
await checkInternetConnection().then((isAvailable) async {
if (isAvailable) {
//ToDO showProgress Bar
final response = await http.post(SIGN_OUT_URL, headers: {
"Accept": "application/json",
}, body: {
"userid": userId,
"login_type": loginType
});

print("Response is" +
response.body.toString() +
"\nfgfdgfdgdf" +
response.request.toString());

final parsedJson = json.decode(response.body);
signOutModel = SignOutModel.fromJson(parsedJson);
if (signOutModel.status == 1) {

//ToDO hide Progress Bar
} else {
//ToDO hide Progress Bar
showMessageDialog(signOutModel.msg, context);
}
} else {
showMessageDialog(CHECK_YOUR_INTERNET_CONNECTION, context);
}
});
}

但每当发出请求时,我都会收到以下错误

[

ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter ( 7932): FormatException: Unexpected character (at character 1)
E/flutter ( 7932): <!DOCTYPE html>
E/flutter ( 7932): ^
E/flutter ( 7932):
E/flutter ( 7932): #0 _ChunkedJsonParser.fail (dart:convert/runtime/libconvert_patch.dart:1358:5)
E/flutter ( 7932): #1 _ChunkedJsonParser.parseNumber (dart:convert/runtime/libconvert_patch.dart:1254:9)
E/flutter ( 7932): #2 _ChunkedJsonParser.parse (dart:convert/runtime/libconvert_patch.dart:922:22)
E/flutter ( 7932): #3 _parseJson (dart:convert/runtime/libconvert_patch.dart:29:10)
E/flutter ( 7932): #4 JsonDecoder.convert (dart:convert/json.dart:540:36)
E/flutter ( 7932): #5 JsonCodec.decode (dart:convert/json.dart:167:41)
E/flutter ( 7932): #6 BaseScreenState.signOut.<anonymous closure> (package:dice_clutter/screens/base/base_screen.dart:223:33)
E/flutter ( 7932): <asynchronous suspension>
E/flutter ( 7932): #7 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7932): #8 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7932): #9 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7932): #10 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7932): #11 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7932): #12 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7932): #13 _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
E/flutter ( 7932): #14 _completeOnAsyncReturn (dart:async/runtime/libasync_patch.dart:295:13)
E/flutter ( 7932): #15 checkInternetConnection (package:dice_clutter/helpers/internet_connection.dart)
E/flutter ( 7932): <asynchronous suspension>
E/flutter ( 7932): #16 BaseScreenState.signOut (package:dice_clutter/screens/base/base_screen.dart:208:11)
E/flutter ( 7932): <asynchronous suspension>
E/flutter ( 7932): #17 BaseScreenState.openScreen (package:dice_clutter/screens/base/base_screen.dart:202:7)
E/flutter ( 7932): #18 BaseScreenState.build.<anonymous closure>.<anonymous closure> (package:dice_clutter/screens/base/base_screen.dart:90:19)
E/flutter ( 7932): #19 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
E/flutter ( 7932): #20 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
E/flutter ( 7932): #21 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 7932): #22 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter ( 7932): #23 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
E/flutter ( 7932): #24 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 7932): #25 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:184:20)
E/flutter ( 7932): #26 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
E/flutter ( 7932): #27 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
E/flutter ( 7932): #28 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 7932): #29 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
E/flutter ( 7932): #30 _invoke1 (dart:ui/hooks.dart:168:13)
E/flutter ( 7932): #31 _dispatchPointerDataPacket (dart:ui/hooks.dart:122:5)

该 api 通过 postman 正常工作,我已经检查了我在 post 请求中输入的 url 和参数它们与 postman 的相同,但我仍然不断收到错误。

最佳答案

您的请求正文不是 JSON 字符串。您必须首先使用 dart:convert 包转换它:

import 'dart:convert';
..
var body = jsonEncode({
"userid": userId,
"login_type": loginType
})
final response = await http.post(SIGN_OUT_URL, headers: {
"Accept": "application/json",
}, body: body
);

关于http - Flutter http post 出错但在 postman 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54425328/

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