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
  • 7,952
  • 3
  • 35
  • 60

2 Answers2

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
  • 721
  • 6
  • 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.