15
votes

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

2
Please ensure network is avialable in your device..and still you are getting the exception ? - Shyju M
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 - Hadrien Lejard
@ShyjuM I have the network as I can access the same URL from the device browser :) - jaumard
@HadrienLejard I try this one first :) but same error - jaumard
Yes I followed this :) I found the issue, I had to add the header "accept:application/json", after that it was all good... - jaumard

2 Answers

22
votes

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.

3
votes

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.