flutter使用Android模拟器调用地址为127.0.0.1的本地接口出现问题
自己写的后端接口,调用地址为本地的http://127.0.0.1,在ios模拟器上调用接口时正常,但在Android模拟器上调用接口出现报错。调用接口时不用localhost或者127.0.0.1,使用自己电脑的IP地址(192.168.xxx.xxx)是因为Android模拟器本身把自己当做了localhost或127.0.0.1,所以会抛出异常。
·
记录:
问题
自己写的后端接口,调用地址为本地的http://127.0.0.1,在ios模拟器上调用接口时正常,但在Android模拟器上调用接口出现报错
E/flutter ( 4257): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection refused
E/flutter ( 4257): #0 IOClient.send (package:http/src/io_client.dart:88:7)
E/flutter ( 4257): <asynchronous suspension>
E/flutter ( 4257): #1 BaseRequest.send (package:http/src/base_request.dart:133:22)
E/flutter ( 4257): <asynchronous suspension>
E/flutter ( 4257): #2 httpInstance (package:kanban/utils/http.dart:20:36)
E/flutter ( 4257): <asynchronous suspension>
E/flutter ( 4257): #3 _LoginPageState.loginUser (package:kanban/pages/login/login.dart:94:18)
E/flutter ( 4257): <asynchronous suspension>
是因为Android模拟器本身把自己当做了localhost或127.0.0.1,所以会抛出异常。
解决方法:
调用接口时不用localhost或者127.0.0.1,使用自己电脑的IP地址(192.168.xxx.xxx)
更多推荐
已为社区贡献2条内容
所有评论(0)