我正在尝试向其响应类似于以下内容的网址发出发布请求
{"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通过邮递员正常工作,并且我检查了我在邮寄请求中输入的网址和参数 和邮递员一样,但我仍然不断收到错误消息。
答案 0 :(得分:4)
您的请求正文不是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
);