【发布时间】:2019-03-08 09:44:19
【问题描述】:
我想连接我的本地主机并使用我的参数获取一个令牌。当我使用此代码在我的设备中连接时,我遇到错误连接被拒绝!
这是我的代码。
class _MyHomePageState extends State<MyHomePage> {
var formContent = {
"grant_type": "1",
"branchcode": "0",
"password": "1",
"username": "1",
"dbname": "1",
"dbuser": "1",
"dbpassword": "1",
"dbtype": "0"
};
Future<String> getData() async {
var postBody= json.encode(formContent);
var response = await http.post(
Uri.encodeFull("http://localhost:7070/api/v2/token"),
body:postBody,
headers: {"Accept": "application/json"});
if(response.statusCode == 200){
return String.fromCharCode(json.decode(response.body));
}else{
throw Exception('Failed');
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new RaisedButton(
child: new Text("Get The Data"),
onPressed: getData,
),
),
);
}
}
我无法将我的本地主机与我自己的值连接起来。我有一个错误,比如连接被拒绝,但我不明白为什么?
Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 40109
【问题讨论】:
-
您知道
localhost是手机/模拟器,而不是您的开发PC?如果您尝试连接的服务在该 IP 上侦听并且您的手机连接到同一网络 (WiFi),则您可以使用adb创建重定向或使用 PC 的公共 IP 地址从手机连接。 -
是的,我知道,那我怎么解决呢?
-
stackoverflow.com/questions/49855754/… 可能会有所帮助,而不是如上所述使用
localhost。 -
我无法理解我如何使用这个 adb reverse —list 命令
-
我不明白我到底在做什么
标签: android ios dart flutter socketexception