0

I want to connect my localhost and take a token with my parameters.When I was connecting in my device with this code I have an error connection refused!

Here is my code.

class _MyHomePageState extends State<MyHomePage> {

      var formContent = {
        "grant_type": "1",
        "branchcode": "0",
        "password": "1",
        "username": "1",
        "dbname": "1",
        "dbuser": "1",
        "dbpassword": "1",
        "dbtype": "0"
      };

      Future<String> getData() async {
        var postBody= json.encode(formContent);
        var response = await http.post(
            Uri.encodeFull("http://localhost:7070/api/v2/token"),
            body:postBody,
            headers: {"Accept": "application/json"});
        if(response.statusCode == 200){
          return String.fromCharCode(json.decode(response.body));
        }else{
          throw Exception('Failed');
        }
      }

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new RaisedButton(
              child: new Text("Get The Data"),
              onPressed: getData,
            ),
          ),
        );
      }
    }

I cannot connect my localhost with my own values. I have an error like connection refused but I don't understand why ?

Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 40109
Md. Sabbir Ahmed
  • 850
  • 8
  • 22
Kaan Karamanoğlu
  • 189
  • 2
  • 5
  • 17

1 Answers1

0

The error given is usually caused either by the device/emulator having no network connectivity, or by the given endpoint being unreachable (i.e. server is offline, incorrect address). If the issue was caused by the latter, you can try replacing the root address from 'localhost' to '10.0.2.2'. This is because the emulator intercepts the address and translates it to '127.0.0.1' - more details about this is explained here https://developer.android.com/studio/run/emulator-networking

Omatt
  • 8,564
  • 2
  • 42
  • 144