16

I'm using the last flutter version on a fresh created project. I'm trying to call this URL https://jsonplaceholder.typicode.com/users

But on iOS or Android I get flutter: Error SocketException: OS Error: Connection refused, errno = 61, address = jsonplaceholder.typicode.com, port = 52988

Here is my network call:

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:test_flutter/constants.dart';
import 'package:test_flutter/users/models/user.dart';

class UserNetworkDatasource {
  Future<List<User>> retrieve() async {
    var httpClient = HttpClient();
    var uri = new Uri.https(baseUrl, '/users');
    var request = await httpClient.getUrl(uri);
    var response = await request.close();
    var responseJson = await response.transform(utf8.decoder).join();
    List userMap = json.decode(responseJson);

    return userMap.map((jsonUser) => User.fromJson(jsonUser));
  }
}

Is there anything to do more than this ? I check the Android manifest and it has the Internet permission so should be ok

Flutter 0.3.2 • channel beta

Framework • revision 44b7e7d3f4 (4 weeks ago) • 2018-04-20 01:02:44 -0700

Engine • revision 09d05a3891

Tools • Dart 2.0.0-dev.48.0.flutter-fe606f890b

jaumard
  • 8,202
  • 3
  • 40
  • 63

3 Answers3

23

I had the same error, but only on release build (android). In the android folder under app/src are 3 folders: debug, main and profile, each containing AndroidManifest file. The one in debug folder had internet permission, but the one in main did not and that was causing the error.

Ethirallan
  • 841
  • 7
  • 9
  • 1
    main, debug and profile all manifest files has internet permission in my project, still getting socket exception error on real devices. – s.j Apr 16 '21 at 05:14
  • @s.j did you try running "flutter clean" after updating AndroidManifest files? – Ethirallan Apr 16 '21 at 15:19
  • @s.j sorry, but with provided information I really can not help you. – Ethirallan Apr 27 '21 at 18:21
  • Similar situation in that the error only occurs on android, but all three AndroidManifest.xml files (debug, main, and profile) have internet permissions. Testing on Android 6, not sure if that's apart of the issue. – tim-montague Feb 10 '22 at 04:43
3

Try to go to the url from the phone. I had the same issue, I was using python http.server for hosting a json file. First I was giving me the same exception, because I bind it with a predefined url. And my emulator couldn't reach the url.

-1

We(myself and my girlfriend) was running a Android Phone (Not emulator) and we was using a WebService on our local server on the same network with Android Phone, but the error was in the port of our local server, it was not open.

In our local server running Ubuntu 22.04.2 LTS.

sudo ufw allow <SERVER_PORT>

You can search how to open a port in your OS and see if it works for you.

for Linux: https://www.digitalocean.com/community/tutorials/opening-a-port-on-linux#for-ubuntu-users-and-ufw-based-systems

for MacOS:https://www.macworld.com/article/671729/mac-firewall-how-to-open-specific-ports-in-os-x-10-10-firewall.html

for Windows: https://learn.microsoft.com/en-us/sql/reporting-services/report-server/configure-a-firewall-for-report-server-access?view=sql-server-ver16