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

AndroidHttpDartFlutterSocketexception

Android Problem Overview


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!

Android Solutions


Solution 1 - Android

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 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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 - Android

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 - Android

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

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

enter image description here

Solution 8 - Android

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 - Android

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

Solution 10 - Android

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

Solution 11 - Android

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 - Android

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

Solution 13 - Android

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

Solution 14 - Android

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 - Android

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 - Android

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 17 - Android

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

Just enable internet connexion

Solution 18 - Android

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 19 - Android

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.

Solution 20 - Android

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.

Solution 21 - Android

  • 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

Solution 22 - Android

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

Solution 23 - Android

Double-check / make sure you have

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

Solution 24 - Android

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.

Solution 25 - Android

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

Solution 26 - Android

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

Solution 27 - Android

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

Solution 28 - Android

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;
}

Solution 29 - Android

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.

Solution 30 - Android

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

Solution 31 - Android

Translated: In my case, the computer had internet, but the emulator did not. I configured the connection on the emulator and solved it.

Solution 32 - Android

check your url address , In my case I was using a wrong url.

Solution 33 - Android

In My case I opened the emulator before I connected to the WIFI to system.When network(WIFI) has connected to my system but unable to connect to emulator . Due to no internet access to the Emulator,this issues occurs.I resolved this issues by this way.

Solutions:

Connected network access to the system then I Closed the emulator and re-open it. Install applications ,run application and open it .And Finally works every API works fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - AndroidIshwar Chandra TiwariView Answer on Stackoverflow
Solution 2 - AndroidFrancis CView Answer on Stackoverflow
Solution 3 - AndroidLiping XiongView Answer on Stackoverflow
Solution 4 - AndroidSameh KhemiraView Answer on Stackoverflow
Solution 5 - AndroidNaimatullahView Answer on Stackoverflow
Solution 6 - AndroidFitsum FirewView Answer on Stackoverflow
Solution 7 - AndroidUmer Waqas CEO FluttydevView Answer on Stackoverflow
Solution 8 - AndroidBalocodesView Answer on Stackoverflow
Solution 9 - AndroidFrancis QuinnView Answer on Stackoverflow
Solution 10 - AndroidJoel D'souzaView Answer on Stackoverflow
Solution 11 - AndroidAhsanView Answer on Stackoverflow
Solution 12 - AndroidFarwaView Answer on Stackoverflow
Solution 13 - AndroidiheathersView Answer on Stackoverflow
Solution 14 - AndroidPranayView Answer on Stackoverflow
Solution 15 - AndroidEzzeddine EssamView Answer on Stackoverflow
Solution 16 - AndroidVincent GagnonView Answer on Stackoverflow
Solution 17 - AndroidM E S A B OView Answer on Stackoverflow
Solution 18 - AndroidislamuxView Answer on Stackoverflow
Solution 19 - AndroidSachin MishraView Answer on Stackoverflow
Solution 20 - AndroidBATMAN_2008View Answer on Stackoverflow
Solution 21 - AndroidNikitaView Answer on Stackoverflow
Solution 22 - AndroidNelson PereiraView Answer on Stackoverflow
Solution 23 - AndroidRiad HossainView Answer on Stackoverflow
Solution 24 - AndroidRyanView Answer on Stackoverflow
Solution 25 - AndroidRoninS28View Answer on Stackoverflow
Solution 26 - AndroidAlemoh Rapheal BajaView Answer on Stackoverflow
Solution 27 - AndroidFlorian KView Answer on Stackoverflow
Solution 28 - AndroidDavidView Answer on Stackoverflow
Solution 29 - AndroidHoxtygenView Answer on Stackoverflow
Solution 30 - AndroidGuillem PucheView Answer on Stackoverflow
Solution 31 - Androidjadson santosView Answer on Stackoverflow
Solution 32 - AndroidAnime-for-3 -dayView Answer on Stackoverflow
Solution 33 - AndroidRahul KushwahaView Answer on Stackoverflow