How to solve SocketException: Failed host lookup: 'www.xyz.com' (OS Error: No address associated with hostname, errno = 7)

Whenever I try to do an http call after about 20 seconds I get in the console the following error:

E/flutter ( 8274): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter ( 8274): SocketException: Failed host lookup: 'flutter-project-xxxxx.firebaseio.com' (OS Error: No address associated with hostname, errno = 7)

This error happens for every method and every route I call through the app http package.

I’m developing a flutter app on Windows, using an AVD virtual device from Android Studio.

Versions:

  http: ^0.12.0+1
  flutter: 1.0

Cases:

  1. From home or though tethering from my phone:
    connectivity works fine on every part of the virtual device

  2. From my work network (behind the firewall):

Only with Android Web View, I can browse the internet without any problem (i can even call the same url, I use in the code and it works).
I get a connection error when using any other application (Chrome, Google Play, ecc…) in the virtual device, and specifically the “SocketException”, when testing my app.

What is the difference between the calls coming from that app?
Is there a way to route my app calls the same way as the ones in the webview?

Thanks!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Adding internet permission is not only a solution.

You also have to make sure that you are online whether it is mobile or emulator

  1. Make sure you are online whether it is mobile or emulator

  2. Make sure you have given internet permission in your app’s android/app/src/main/AndroidManifest.xml

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

Solution 2

Try adding <uses-permission android:name="android.permission.INTERNET" /> to your AndroidManifest.xml file.

Solution 3

If you are using an emulator make sure that the mobile data is active

Solution 4

I added <uses-permission android:name="android.permission.INTERNET" /> to
my manifest. I then had to restart the emulator for internet to work.

Solution 5

You should try following:

Try run application in debug mode if its working fine and not working in release mode then it might be related to permission issue so add permissions(INTERNET, ACCESS_NETWORK_STATE) in manifest file.

  • your_project_root_directory/android/app/src/debug/AndroidManifest.xml
  • your_project_root_directory/android/app/src/main/AndroidManifest.xml
  • your_project_root_directory/android/app/src/profile/AndroidManifest.xml

If not included then add permissions:

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

If its not working in debug mode, then check either your url is correct by hitting in postman. If its working in postman then you should execute your webservice in this way:

Add dependency in pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter

  http: 0.13.3

After adding http:0.13.3 then click on Pub get button If its executed with code 0 then it means its successfully added to your project.

in your dart file import http.dart:

import 'package:http/http.dart';

Now write this code to execute webservice:

var url = Uri.parse('some-url-login?username=abc&&password=xyz');
var response = await get(url, headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': 'true',
          'Access-Control-Allow-Headers': 'Content-Type',
          'Access-Control-Allow-Methods':
          'GET,PUT,POST,DELETE'
        });
if (response.statusCode == 200) {
 // success
} else if(response.statusCode == 404){
 // not found
} else if(response.statusCode == 500){
 // server not responding.
} else {
 // some other error or might be CORS policy error. you can add your url in CORS policy. 
}

I hope it will help you. Chill!

Solution 6

Go for AVD manager, then choose your emulator, on Actions menu click down option sign, then choose wipe data, then restart your emulator. It works for me.

Solution 7

Make sure you have added the in android/app/src/main/AndroidManifest.xml

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

How to solve SocketException: Failed host lookup: 'www.xyz.com' (OS Error: No address associated with hostname, errno = 7)

Solution 8

If you are getting this error while using the flutter_socket_io plugin, don’t forget to initialize the socket i.e socketIO.init(); socketIO.connect(); before attempting to subscribe to it. I received the same error as that indicated above when I omitted it.

Solution 9

Make sure your Mobile device or Emulator is connected to the internet

Solution 10

Even I had the same issue. I restarted my emulator and it worked!!

Solution 11

By enabling WIFI it works for me, and make sure you also added the internet permission in the android manifest it added automatically also but you have to make sure. if still not then invalidate caches and restart hope it works

Solution 12

Check if the wifi is connected or not. Mine worked after restarting the wifi.

Solution 13

When i enabled wifi on my device, it worked fine.

Solution 14

Even after adding

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

in your android/app/src/main/AndroidManifest.xml, if you’re still finding trouble then

There maybe an error with the website’s server IP address like:

Server IP address could not be found

It’s better to wait for some time and make sure to restart wifi, it worked for me.

Solution 15

Make sure your Emulator or your device connected to the internet.
if your Emulator could not connect to the internet use a VPN.

Solution 16

For me it was just bad url with function "Uri.https(…)" in Flutter.
Check if the address is correct with your browser

Solution 17

The problem for me was that I was using a VPN. The emulator didn’t have internet access when connected to the VPN on my computer. It might help someone.

Solution 18

In my case it was because my phone was not connected to internet

Just enable internet connexion

Solution 19

I’m using arch Linux.

Linux arch 5.14.7-arch1-1

And Flutter version

Flutter 2.2.1 

Dart version

Dart 2.13.1

Code editor

1.58.0

In my case turning on wifi solved the problem.

Solution 20

The problem is basically coming when device is trying to access any internet resources,to fix add the
to AndroidManifest.xml file @android/app/src location.

Method 21

Check if you have enabled offline work in the gradle settings.
File-> Settings -> Build -> Build Tools -> Offline work

If its enabled or checked then uncheck it.

Method 22

  • Make sure the internet is on (both on the phone and the PC)
  • also, make sure the server is working properly
  • check your URL properly

Method 23

Also, might good to look at the ‘Proxy settings’ inside the ‘Extended controls’ of the emulator.

The way it worked for me was to change to use the ‘No Proxy’ setting.

enter image description here

Method 24

Double-check / make sure you have

<uses-permission android:name="android.permission.INTERNET" />
in your app’s android/app/src/main/AndroidManifest.xml

Method 25

Sometimes, other wifi can have interference with your connected wifi, which can cause this error as well. Try to set your connected wifi’s channel to auto or away from other wifi’s channels.

Method 26

I had the same problem while running the app on an emulator.

My laptop was connected to the internet through my phone’s mobile hotspot.
But apparently the emulator was unable to access the internet

Later, I switched to my Wi-Fi Router connection and the application proceeded smoothly as expected.

I’m not sure, but is there any special access to be granted?

Tip: Try googling something on the emulator to check internet connectivity

Method 27

if you’re using vscode you would get gai_error=0 that’s if the http is connected successfully

Here is the right place to place the internet permission because it was quite challenging to know the location

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appName">
<uses-permission android:name="android.permission.INTERNET" />

run the flutter run command on cmd or visual studio code terminal

Method 28

I deliberately disconnected my emulator wifi to see what would happen (as there could be cases where no internet is available). The error led me here, but, all I see are workarounds and not error handling. Really, the app should be able to handle the error gracefully and not crash out because of a lack of connection.

So far, all I have done is to catch the error and if the return code is -1 (or anything other than a 200), I know I have a problem and can make the app respond appropriately.

var request = new http.MultipartRequest("POST", uri);

// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
    filename: fileToUpload.path);

// add file to multipart
request.files.add(multipartFile);

// send
try {
  var response = await request.send();
  print(response.statusCode);

  // listen for response
  response.stream.transform(utf8.decoder).listen((value) {
    print(value);
  });

  return response.statusCode;
}
catch (e)
{
  return -1;
}

Method 29

It’s definitely internet issue as others have stated. @petro and @Tushar Nikam response put me in the right direction. My emulator was in airplane mode, but I don’t know how; it’s the one I use 90% of the time. I’d advise to check for anything that might block internet connectivity.

Method 30

For me, the problem was that my emulator had network connections (LTE and Wi-Fi) disabled.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply