自从今天起,每当我尝试登录我的应用程序时,Dio包会抛出以下错误:SocketException: Insecure socket connections are disallowed by platform: 10.0.2.2
我使用以下设置进行连接:
static BaseOptions options = new BaseOptions(
baseUrl: "http://10.0.2.2:3000", // on android emulator
connectTimeout: 5000,
receiveTimeout: 3000)
因此,大致如下(在我的身份验证设置和正常工作的/user/login):
var apiLogin = api.dio;
try {
Response response = await apiLogin.post("/user/login",
options: Options(contentType: "application/json"),
data: {"email": email, "password": password});
} on DioError catch (e) {
throw Exception([e]);
}
我有一个运行在3000端口的Node服务器,它连接着容器化的mongodb。当尝试进行身份验证时,它立即出现了DioError错误,我无法在任何在线资源中找到原因。
有人知道这个错误与什么相关吗?
编辑[答案]
感谢@lyrics指引我正确的方向: 从API级别27及以上,usesCleartextTraffic默认为false,因此阻止了出站http请求,需要使用HTTPS。
解决方案是将以下内容添加到AndroidManifest.xml中:
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
来源: https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic