Http: Not working for localhost URLs on Android

Created on 24 Jan 2019  路  5Comments  路  Source: dart-lang/http

I was successfully able to call my localhost url: http://localhost:3030/ping on iOS, but the same code threw and error on Android:

My code

    String thisUrl = 'http://localhost:3030/ping';
    try {
      var response = await http.get(thisUrl);
    } catch(err) {
      Scaffold.of(context).showSnackBar(SnackBar(content: Text('Could not connect to server')));
      return;
    }

Most helpful comment

To connect your localhost (127.0.0.1) with android emulator devices you should use http://10.0.2.2/

All 5 comments

What is the error?

Is the server running?

The server is running. I can call is successfully on iOS. I get this error on Android:

SocketException (SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 59278)

The weird thing is the url is for port 3030 but that error says the port is 59278.

try using your machine's network IP, i.e. http://192.168.1.11:3030

To connect your localhost (127.0.0.1) with android emulator devices you should use http://10.0.2.2/

@IrosTheBeggar - the strange looking port is caused by https://github.com/dart-lang/sdk/issues/12693

Closing the remainder of this issue as a duplicate of https://github.com/flutter/flutter/issues/29688 since flutter should own the documentation for using http in a flutter application and emulator.

Was this page helpful?
0 / 5 - 0 ratings