0

I have created rest api from .net. I have simple login and registration android application. Here is code where I bind local ip

public class RetrofitClient {
    private static Retrofit instance;
    public static Retrofit getInstance() {
        if (instance == null)
            instance = new Retrofit.Builder()
                    .baseUrl("http://localhost:5000/")
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        return instance;
    }
}

When I run the application and test my login function it return

Failed to connect to localhost/127.0.0.1:5000

How can I solve this issue

emulator display

emulator setting

Rahul
  • 405
  • 1
  • 4
  • 15
  • are you sure with port number? – Abdul Waheed Feb 03 '21 at 07:26
  • Did you try [this](https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator)? – Muhammad Farhan Feb 03 '21 at 07:27
  • @AbdulWaheed Yes I typed wrong output early I updated! – Rahul Feb 03 '21 at 07:30
  • @MuhammadFarhan Yes I tried with my machine ip address which is 192.168.x.x something like that – Rahul Feb 03 '21 at 07:30
  • Can you confirm what IP is being displayed in emulator. You can confirm this in emulator settings. – Abdul Waheed Feb 03 '21 at 07:36
  • 1
    And please confirm what OS version you are using? – Abdul Waheed Feb 03 '21 at 07:37
  • @AbdulWaheed added! – Rahul Feb 03 '21 at 07:42
  • `Here is code where I bind local ip` No that code does not bind a socket to an ip. Instead it is code that tries to connect with a server on localhost. As every device or emulator is its own localhost that code (which runs on an emulator) tries to connect with a server running on that emulator too. Well did you install your server on your emulator? – blackapps Feb 03 '21 at 09:37
  • @blackapps install server mean? I run server locally... API working fine I have tested using Postman – Rahul Feb 03 '21 at 09:52
  • Locally? I have no idea where that would be. Locally on your emulator? Locally on your Android phone? Localhost is the device itself. Localhost is the emulator itself. – blackapps Feb 03 '21 at 09:58
  • My API is localhost:5000 I need to connect my API with emulator – Rahul Feb 03 '21 at 10:01

2 Answers2

0

You need create an xml and write below code in that xml file

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">your sub domain</domain>
</domain-config>
</network-security-config>

The location of the file should be as

** main/res/xml/network_security_config.xml**

And add this file in manifest in application tag as mentioned below:

<application
......
android:networkSecurityConfig="@xml/network_security_config">

Explanation:

Since localhost is not a secure domain, you must allow non-secured domains in your network configuration for API 28+ where non-TLS connections are prevented by default.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

In my case server run on localhost means 127.0.0.1:5000. I have change this to my local IP address which is 192.168.x.x:5000 to run the server. Then it worked. Hope this will helpful.

Rahul
  • 405
  • 1
  • 4
  • 15