Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making Network http error SocketException: Failed host lookup #27883

Closed
hoc081098 opened this issue Feb 13, 2019 · 17 comments

Comments

Projects
None yet
7 participants
@hoc081098
Copy link

commented Feb 13, 2019

Steps to Reproduce

Making Network http error when build release, even having internet connection
When build debug having no error

Error: SocketException: Failed host lookup: 'jsonplaceholder.typicode' (OS  Error No address associated with hostname, error = 7)

Code:

main.dart:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.dark(),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const url = 'https://jsonplaceholder.typicode.com/users';

  Future<List<Map<String, dynamic>>> _future;

  @override
  void initState() {
    super.initState();
    _future = fetch();
  }

  Future<List<Map<String, dynamic>>> fetch() {
    return http
        .get(url)
        .then((response) {
          return response.statusCode == 200
              ? response.body
              : throw 'Error when getting data';
        })
        .then((body) => json.decode(body))
        .then((list) => (list as List).cast<Map<String, dynamic>>());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: RefreshIndicator(
        onRefresh: () async {
          _future = fetch();
          setState(() {});
          return _future;
        },
        child: FutureBuilder<List<Map<String, dynamic>>>(
          future: _future,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Center(
                child: Container(
                  constraints: BoxConstraints.expand(),
                  child: SingleChildScrollView(
                    physics: AlwaysScrollableScrollPhysics(),
                    child: Text(snapshot.error.toString()),
                  ),
                ),
              );
            }

            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            return ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                final item = snapshot.data[index];
                return ListTile(
                  title: Text(item['name']),
                  subtitle: Text(item['email']),
                );
              },
            );
          },
        ),
      ),
    );
  }
}

Screenshots:

Lenovo a2010: android 5.1

Philips S337: android 5.1:

Logs

flutter analyze

Analyzing test_http...
No issues found! (ran in 3.5s)

flutter doctor -v

[√] Flutter (Channel dev, v1.2.0, on Microsoft Windows [Version 10.0.17134.523], locale en-US)
    • Flutter version 1.2.0 at C:\flutter\flutter
    • Framework revision 06b979c4d5 (3 weeks ago), 2019-01-25 14:27:35 -0500
    • Engine revision 36acd02c94
    • Dart version 2.1.1 (build 2.1.1-dev.3.2 f4afaee422)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at C:\Users\Admin\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: E:\Admin\Other\android-preview\android-studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[!] Android Studio (version 3.3)
    • Android Studio at E:\Admin\Other\android-preview\android-studio
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[√] IntelliJ IDEA Community Edition (version 2018.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3
    • Flutter plugin version 32.0.3
    • Dart plugin version 183.5153.38

[√] VS Code (version 1.31.0)
    • VS Code at C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 2.22.3

[√] Connected device (1 available)
    • Philips S337 • FS011641B06736 • android-arm • Android 5.1 (API 22)
@zoechi

This comment has been minimized.

Copy link
Contributor

commented Feb 13, 2019

Are you using a proxy?

@hoc081098

This comment has been minimized.

Copy link
Author

commented Feb 15, 2019

No. I don't use proxy

@zoechi

This comment has been minimized.

Copy link
Contributor

commented Feb 15, 2019

Hard to tell. Looks like a network issue.
Do other remote or local network adresses work?

@hoc081098

This comment has been minimized.

Copy link
Author

commented Feb 19, 2019

I tried many network but still not working

@zoechi

This comment has been minimized.

Copy link
Contributor

commented Feb 20, 2019

Shouldn't this be jsonplaceholder.typicode.com instead of jsonplaceholder.typicode?

@zoechi

This comment has been minimized.

Copy link
Contributor

commented Feb 20, 2019

Please consider asking support questions in one of the other channels listed at http://flutter.io/support .

@zoechi zoechi closed this Feb 20, 2019

@hoc081098

This comment has been minimized.

Copy link
Author

commented Feb 20, 2019

i think that url is not a problem.
paste url https://jsonplaceholder.typicode.com/users in browser working well
static const url = 'https://jsonplaceholder.typicode.com/users'; in class _MyHomePageState

@zoechi zoechi reopened this Feb 20, 2019

@zoechi

This comment has been minimized.

Copy link
Contributor

commented Feb 20, 2019

@mraleph could this be a tree-shaking issue or something like that?
(sorry, don't know whom else to summon)

@zoechi zoechi added this to the Goals milestone Feb 20, 2019

@hoc081098

This comment has been minimized.

Copy link
Author

commented Feb 20, 2019

i rebuild release app and this is error:

E/flutter (11529): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
E/flutter (11529):      BAD_SIGNATURE(ecdsa.c:247)
E/flutter (11529):      BAD_SIGNATURE(handshake_client.cc:1060))
E/flutter (11529): #0      IOClient.send (package:http/src/io_client.dart:33:23)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #2      BaseClient.get (package:http/src/base_client.dart:32:7)
E/flutter (11529): #3      get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter (11529): #4      _withClient (package:http/http.dart:166:20)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #5      get (package:http/http.dart:46:5)
E/flutter (11529): #6      _MyHomePageState.fetch (package:test_http/main.dart:37:12)
E/flutter (11529): #7      _MyHomePageState.build.<anonymous closure> (package:test_http/main.dart:56:21)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #8      RefreshIndicatorState._show.<anonymous closure> (package:flutter/src/material/refresh_indicator.dart:357:53)
E/flutter (11529): #9      _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (11529): #10     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (11529): #11     _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter (11529): #12     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)

E/flutter (11529): #13     Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter (11529): #14     Future._completeWithValue (dart:async/future_impl.dart:483:5)
E/flutter (11529): #15     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:513:7)
E/flutter (11529): #16     _rootRun (dart:async/zone.dart:1124:13)
E/flutter (11529): #17     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (11529): #18     _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (11529): #19     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
E/flutter (11529): #20     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (11529): #21     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (11529):
@zarko-tg

This comment has been minimized.

Copy link

commented Mar 10, 2019

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

@hoc081098

This comment has been minimized.

Copy link
Author

commented Mar 12, 2019

Thank @zarko-tg.

Solve: add <uses-permission android:name="android.permission.INTERNET" /> to android/app/src/main/AndroidManifest.xml

@ferso

This comment has been minimized.

Copy link

commented Apr 26, 2019

Thank @zarko-tg.

Solve: add <uses-permission android:name="android.permission.INTERNET" /> to android/app/src/main/AndroidManifest.xml

This saves me, Why this is not in the documentation ?

@zarko-tg

This comment has been minimized.

Copy link

commented Apr 26, 2019

@ferso It'd be less to worry about or consult documentation if they'd make it a default (in templates/starters) since most apps connect online. I spent like a day on a silly little thing.

@xai1983kbu

This comment has been minimized.

Copy link

commented Apr 30, 2019

I had that error even in Debug mode (android/app/src/debug/AndroidManifest.xml has
<uses-permission android:name="android.permission.INTERNET" />)

Adding internet permission is not only a solution,
You also have to make sure that you are online whether it is mobile or emulator (c)
https://stackoverflow.com/a/55548864/9783262

I wiped data
C:\Users\UN\AppData\Local\Android\Sdk\emulator> .\emulator -avd Nexus_5X_API_28 -wipe-data
(or via Android Studio)
and run main.dart

It works!
p.s. it's not related to release but I hope it will save time to someone.

@RobertB4

This comment has been minimized.

Copy link

commented May 11, 2019

Adding <uses-permission android:name="android.permission.INTERNET" /> solved this issue for me.

The documentation is out of date and should be updated. Since Flutter uses the internet permission internally in development mode it is always active when developing. When deploying the app to the play store, it is not active by default.

The documentation currently says:

uses-permission: Remove the android.permission.INTERNET permission if your application code does not need Internet access. The standard template includes this tag to enable communication between Flutter tools and a running app.

Which is misleading since the internet permission isn't in the production manifest (apparently it was in earlier flutter versions?). Please add a note to this so the documentation accurately explains that the internet permission has to be added to the production manifest in case it is needed.

@atapas

This comment has been minimized.

Copy link

commented May 25, 2019

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

Great Info. This helped me.

As I was wondering a bit that, where to add this line in that file... figured it out to be at the end like:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.