http request not processing while emulating or running on real device mode but works fine with emulator

6,425

Solution 1

The problem with my firewall. Which blocking my port 9090.

So added the rule for firewall for port and now it works fine. BTW thanks to @rmtmckenzie

Solution 2

This is most likely to do with how you're running your server, as that seems to be right so long as you've put in the right IP address and your device is connected to the same network.

Something that tripped me up once is that if you run your server on 'localhost' or '127.0.0.1', it may not actually be doing what you think it is. Those should both host in a local redirect that only allows access from your own computer. It's likely that the emulator has something set up so it acts like it's connecting to localhost.

If you run the server on 192.168.31.16 instead that will likely be more successful, but then you might not be able to access it from the emulator. So try running on 0.0.0.0:9091 as that means 'all IP addresses on the local machine', if you aren't already.

I'd advise putting in a dummy endpoint (like /healthz) on your server that just returns an html or plain-text page saying "It works" or something like that. Then open your phone's web browser and go there. If it works, you know that your phone is able to communicate with the computer and then we can start looking into coding issues. If it doesn't work, you know it's to do with the server's configuration.

Share:
6,425
Rahul Mahadik
Author by

Rahul Mahadik

Rahul Mahadik is currently working with a leading web development company as a Tech lead. He received his Master’s degree in Computer Engineering from University of Pune. He is passionate about technical blogging, learn and explore new things and almost versatile in terms of programming across various languages & frameworks such as Java, Spring, spring boot, Android, PHP, MySQL, WordPress,Bootstrap, HTML5, CSS3, Java Scripts, jQuery, Grails-Groovy and more.

Updated on December 07, 2022

Comments

  • Rahul Mahadik
    Rahul Mahadik over 1 year

    In my flutter application, i have user registration form which sends the data to an server using http request.

    Whenever I submit the form and sending the request using the emulator (API 27 and android version 8.1) which works fine but in real android device it gives a error on localhost server

    SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 48134

    I am working with Android device with version 7.1

    Code:

    final JsonDecoder _decoder = new JsonDecoder();
    
      Future<dynamic> post() {
        return http.post(
          "http://192.168.31.16:9091/myprj/api/register",
          body: json.encode({
            "username": "rahul",
            "password": "rahul",
            "email": "[email protected]",
          }),
          headers: {"Accept": "application/json","Content-type": "application/json",},
        ).then((http.Response response) {
          final String res = response.body;
          final int statusCode = response.statusCode;
    
          if (statusCode < 200 || statusCode > 400 || json == null) {
            throw new Exception("Error while fetching data");
          }
          return _decoder.convert(res);
        });
      }
    

    Where, i have used local network IP 192.168.31.16 instead of localhost. When i use live server address it works fine using android device also. The problem is only with localhost http request server with real android device.

    Hope you understand my problem.