11

Since today whenever I try to login my application I get the following error thrown by the Dio package: SocketException: Insecure socket connections are disallowed by platform: 10.0.2.2

I use the following settings to connect:

static BaseOptions options = new BaseOptions(
    baseUrl: "http://10.0.2.2:3000", // on android emulator
    connectTimeout: 5000,
    receiveTimeout: 3000)

And consequently something along the lines of (where I have authentication set-up and functioning properly at /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]);
} 

I have a Node server running on port 3000 which is connected to (containerized) mongodb. When trying the authentication, it immediately has the DioError and I haven't been able to find the cause anywhere online.

Does anyone know what this error is related to?

EDIT [ANSWER]

Thanks to @lyrics for pointing me in the right direction: From API level 27 and higher, usesCleartextTraffic defaults to false, consequently blocking outgoing http requests, requiring HTTPS.

The solution was to add the following to AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

as stated in stackoverflow answer

source: https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic

4 Answers4

13

I have a similar issue with Flutter running on ios Simulator and Android emulator:

SocketException: Insecure socket connections are disallowed by platform:

Inside the Flutter project go to :

To enable in Ios:

ios folder -> runner folder -> info.plist

Then add the following lines to enable HTTP requests:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

To enable in Android:

Android folder -> app -> src -> main -> AndroidManifest.xml

Add this permission:

<uses-permission android:name="android.permission.INTERNET"/>

then add the following line inside application tag:

android:usesCleartextTraffic="true"
Mina Farid
  • 5,041
  • 4
  • 39
  • 46
2

First add-in AndroidManifest file

add this line in the end before closing tag

<uses-permission android:name="android.permission.INTERNET" />

add this line in the beginning find this tag and add it first in it

<application
    android:usesCleartextTraffic="true"
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

It seems you newly upgraded to Android 10, Well you probably didn't know that HTTPS is the default connection protocol starting with Android 9 and that all connection not using Https will fail. Read more https://developer.android.com/training/articles/security-config.html

See this answer here https://stackoverflow.com/a/50834600/6467637

lyrics
  • 61
  • 6
0

I was making a call to- Response response = await get('http://worldtimeapi.org/api/timezone/Asia/Kolkata');

Instead of http I simply replaced it to https and it was fixed.

The new request was- Response response = await get('https://worldtimeapi.org/api/timezone/Asia/Kolkata');

This fixed the issue for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Manthan_Admane
  • 431
  • 6
  • 9