我写了一个简单的Flutter应用, 当用户按下按钮时,它将向我的烧瓶服务器启动http GET。这是相关的抖动代码:
Future <String> getData() async {
http.Response resp =
await http.get('http://192.168.1.10:5000/search?search=Hooray', headers: {
HttpHeaders.contentTypeHeader: "application/json",
});
if (resp.statusCode == 200) {
String data = resp.body;
var decodedData = jsonDecode(data);
print(decodedData);
return decodedData;
} else {
int stCode = resp.statusCode;
print('statusCode = $stCode');
print(resp.reasonPhrase);
return (resp.reasonPhrase);
}
}
void performTasks() async {
String res = await getData();
print(res);
}
我的烧瓶服务器在我的Linux机器上运行:
def main():
app.run(host='0.0.0.0', debug=True)
我在Android设备上运行了flutter应用程序,但我不断得到:
未处理的异常:SocketException:操作系统错误:连接超时, errno = 110,地址= 192.168.1.10,端口= 38466
顺便说一句,我已经监视了PC上的入站流量,以检查传入的http get数据包(flask服务器在我的PC上运行),但是那些HTTP GET数据包却找不到到我的PC的方式。
还有两件事要注意:
如果我用一个简单的flutter应用程序包装getData()函数并在运行烧瓶服务器的PC上运行 ,我将得到预期的响应!
防火墙始终不活动(ufw状态为不活动)
答案 0 :(得分:1)
简单的问题。
您正在尝试通过IP使用GET
请求,该请求无效。
我看到您正在本地主机上运行Flask
服务器,可以从您的PC直接访问,但不能从外部访问。因此,您的应用会超时。
解决方案
您需要公开本地主机,以便可以使用外部连接。
建议:
基本上,它们允许外部流量进入本地主机端口
我的测试。
在我的端口node
上使用express js
运行3000
服务器。
但是http://<my_ip>:3000/
不能从外部使用
因此,我在一个单独的终端上运行了命令ssh -R test_user:80:localhost:3000 serveo.net
,该终端仅表示我要公开我的端口3000
,外部连接将连接到https://test_user.serveo.net
(它将在URL之后运行用于外部连接的命令。
现在,我的节点服务器可以看到来自我的localhost
(来自我的电脑)的连接,以及有人访问了https://test_user.serveo.net
对于flutter应用程序
Future <String> getData() async {
http.Response resp =
await http.get('https://test_user.serveo.net', headers: {
HttpHeaders.contentTypeHeader: "application/json",
});
print(resp.body);
//... whatever
}
将向您显示响应。
编辑:
我发现的另一个快速技巧是
在您的计算机上键入ifconfig | grep inet
,这将为您提供类似的信息
inet 192.168.12.1 netmask 255.255.255.0
您还可以使用http://192.168.12.1
从外部进行连接