【发布时间】:2020-05-24 03:14:05
【问题描述】:
如何从 Android 上的 Flutter 应用程序向本地运行的 Firebase 云功能服务器发出 POST 请求?
这是我的代码:
{
print("good email");
String url = 'http://10.0.2.2:5000/mattsapp/api1'; //I have also tried replacing this with localhost
String json = '{"email": "a@gmail.com", "username": "user1", "password":"111111"}';
Future<void> loginPost() async {
http.Response resp = await http.post(url, body: json);
print(resp);
}
loginPost();
}
当我运行代码时,连接超时。当我将10.0.2.2 替换为localhost 时,出现以下错误:
Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 58912
当我和邮递员一起跑到http://localhost:5000/mattsapp/api1 时,它工作正常。
【问题讨论】:
-
尝试在您的请求标头中添加标头:HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded"
-
嗨@Federick Jonathan。我怎么做。我只是在发布请求中添加它。例如:await http.post(url, body: json, header: "application/x-www-form-urlencoded"?
-
http.post(url, body: json, headers: {HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded"},)。我的错,标题应该接收地图
-
还是不行。它仍在运行,只是会超时。
-
是的,它只是超时了。
标签: firebase flutter dart google-cloud-functions