0

I am new to flutter development. I am trying hard to make the https calls work. Whenever I call a https url, it throws the below error:

SocketException: Failed host lookup: 'someserver.com' (OS Error: nodename nor servname provided, or not known, errno = 8)

Below is the code I am using,

final ioc = new HttpClient();
    ioc.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
    final http = new IOClient(ioc);
    return http
        .post(url, body: body, headers: headers, encoding: encoding)
        .then((response) {
      final String res = response.body;
      final int statusCode = response.statusCode;

      if (statusCode < 200 || statusCode > 400 || json == null) {
        throw new Exception("Error while posting data");
      }
      return _decoder.convert(res);
    });

I have checked below few links and many more and tried all of them but nothing seems to work.

Also please note that I am able to access the server from the browser. Same server is being access from iOS app as well(built using XCode). From iOS I am able to access the server.

Kindly help!!!

Guest
  • 121
  • 1
  • 1
  • 8
  • Please add code snippet – Shubham Narkhede Jan 20 '21 at 07:40
  • Are you trying to acess xyz.com ? Post some of your code – dm_tr Jan 20 '21 at 07:41
  • You are saying an iOS app is establishing connection just fine, then you are saying you did create the android manifest. There is no way these two things happened on the same device or emulator. What device are you on and what did you do to make sure *that* device *can* establish a connection to your server in general? – nvoigt Jan 20 '21 at 07:54
  • No what I meant was I have built a similar app for iPhone using XCode and not flutter. Now I am building an app using flutter framework. I am getting server connection issue only on the flutter app. – Guest Jan 20 '21 at 08:43

1 Answers1

0

The http package provides the simplest way to fetch data from the internet.

To install the http package, add it to the dependencies section of the pubspec.yaml file. You can find the latest version of the http package to here

dependencies:
  http: <latest_version>

Import the http package.

import 'package:http/http.dart' as http;

Additionally, in your AndroidManifest.xml file, add the Internet permission.

<uses-permission android:name="android.permission.INTERNET" />

Then like the below function, you will be able to get/fetch/post HTTP call

Future<http.Response> getchData() {
  return http.get('https://jsonplaceholder.typicode.com/albums/1');
}

For Advanced HTTP call and management you can use Dio plugin

Gourango Sutradhar
  • 1,461
  • 10
  • 16
  • I have already done all these but nothing seems to work. Please check the code. I have also added the internet permission in androidmanifest file. – Guest Jan 20 '21 at 07:47
  • then, please check your internet connection + the accessibility of the server. The server maybe down. – Gourango Sutradhar Jan 20 '21 at 07:48
  • I am able to access the server from the browser. – Guest Jan 20 '21 at 07:49
  • then check your internet connection for the phone/emulator. Try again using another device. The problem is clearing that the connect can't be established. – Gourango Sutradhar Jan 20 '21 at 07:50
  • Similar iOS app is also built. I am able to access the server from the iOS app very well. The only issue exists with is flutter app. – Guest Jan 20 '21 at 07:51
  • I think you are having some network issue from the device side, as you cleared that you have followed the HTTP implementation instructions – Gourango Sutradhar Jan 20 '21 at 07:53