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

13
  • Please ensure network is avialable in your device..and still you are getting the exception ? Commented May 16, 2018 at 11:35
  • You should try to use http package pub.dartlang.org/packages/http instead of dart:io directly. More resources here flutter.io/cookbook/networking/fetch-data Commented May 16, 2018 at 11:44
  • @ShyjuM I have the network as I can access the same URL from the device browser :) Commented May 16, 2018 at 11:47
  • @HadrienLejard I try this one first :) but same error Commented May 16, 2018 at 11:47
  • 2
    Yes I followed this :) I found the issue, I had to add the header "accept:application/json", after that it was all good... Commented May 17, 2018 at 4:41

3 Answers 3

24

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.

Sign up to request clarification or add additional context in comments.

4 Comments

main, debug and profile all manifest files has internet permission in my project, still getting socket exception error on real devices.
@s.j did you try running "flutter clean" after updating AndroidManifest files?
@s.j sorry, but with provided information I really can not help you.
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.
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.

Comments

-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.