gpt4 book ai didi

java - 如何将我的 SpringBoot 后端连接到 flutter?

转载 作者:行者123 更新时间:2023-12-05 07:22:44 26 4
gpt4 key购买 nike

我正在开发一个将 spring-boot 后端用于应用程序方法的应用程序,并且我正在使用 flutter 开发我的前端。我制作了我需要的前端页面,但我不确定如何从 flutter 到我的后端进行 HTTP 调用

我尝试查看与我正在做的类似的 youtube 视频和其他堆栈溢出问题,但我仍然无法弄清楚。

比如这是登录鉴权的后台代码

/**
* Controller method that attempts to login
*
* @param username
* @param password
* @return ResponseEntity
*/
@GetMapping("/auth/{username}/{password}")
public ResponseEntity login(@PathVariable("username") String username, @PathVariable("password") String password)
throws Exception {
// No exception thrown means the authentication succeeded
try {
authentication.login(username, password);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Response(false, e.getMessage()));
}
return ResponseEntity.status(HttpStatus.OK).body(new Response(true, "Login successful"));
}

这就是我在 Flutter 中包含的内容

...
String url = 'http://localhost:8080/users/auth/{username}/{password}';

Future<String> login() async {
var response = await http.get(Uri.encodeFull(url), headers:{"Accept" : "application/json"});
Navigator.of(context).pushNamed('/signup');
}
...

...

child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: () {
login();
},
child: Text("Login",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.bold)),
),

...

这是出现的错误

E/flutter ( 3945): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Invalid argument(s): No host specified in URI /auth/%7Busername%7D/%7Bpassword%7D
E/flutter ( 3945): #0 _HttpClient._openUrl (dart:_http/http_impl.dart:2117:9)
E/flutter ( 3945): #1 _HttpClient.openUrl (dart:_http/http_impl.dart:2043:7)
E/flutter ( 3945): #2 IOClient.send (package:http/src/io_client.dart:33:36)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #3 BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #4 BaseClient.get (package:http/src/base_client.dart:32:7)
E/flutter ( 3945): #5 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 3945): #6 _withClient (package:http/http.dart:166:20)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #7 get (package:http/http.dart:46:5)
E/flutter ( 3945): #8 _MyHomePageState.login (package:calcount_mobile/main.dart:65:26)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #9 _MyHomePageState.build.<anonymous closure> (package:calcount_mobile/main.dart:102:11)
E/flutter ( 3945): #10 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:511:14)
E/flutter ( 3945): #11 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:566:30)
E/flutter ( 3945): #12 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)
E/flutter ( 3945): #13 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:240:9)
E/flutter ( 3945): #14 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:177:9)
E/flutter ( 3945): #15 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:436:9)
E/flutter ( 3945): #16 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter ( 3945): #17 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter ( 3945): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:221:19)
E/flutter ( 3945): #19 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:199:22)
E/flutter ( 3945): #20 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 3945): #21 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 3945): #22 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 3945): #23 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 3945): #24 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 3945): #25 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 3945): #26 _invoke1 (dart:ui/hooks.dart:233:10)
E/flutter ( 3945): #27 _dispatchPointerDataPacket (dart:ui/hooks.dart:154:5)
E/flutter ( 3945):

最佳答案

  1. 如果您正在使用 flutter Emulator 设备测试您的 spring boot 后端,那么 url 应该是字符串 url = 'http://10.0.2.2:8080/users/auth/{用户名}/{密码}';

  2. 如果您正在使用真实的 Android 手机测试您的后端,请定义入站防火墙规则(在 windows defender 防火墙中)并在防火墙规则中允许端口 80。获取您系统的IP地址,并将url更改为String url = 'http://ip:8080/users/auth/{用户名}/{密码}';

关于java - 如何将我的 SpringBoot 后端连接到 flutter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56431624/

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