failed to connect to localhost/127.0.0.1 android
Asked Answered
S

7

21

I am new to android development, and trying to call local .NET web api service in android via retrofit library. After starting my web api on IIS I am getting this error failed to connect to localhost/127.0.0.1 android.

When I did same thing as suggested http://themakeinfo.com/2015/04/retrofit-android-tutorial/, It's working fine, But my localhost service is not calling up from android

My service url is, http://localhost:52511/api/Values/getAllStudents/5

and it is giving me output in XML format in browser too.

I have also try to call it with,

public interface gitapi {
    @GET("/api/Values/GetProduct/{id}")      //here is the other url part.best way is to start using /
    public void getFeed(@Path("id") int id, Callback<gitmodel> response);
}

public class gitmodel {
    public int studentId;
    public String studentName;
    public String studentAddress;
}


String API = "http://10.0.2.2:52511";
public void CallService(View view){

        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(API).build();
        gitapi git = restAdapter.create(gitapi.class);

        int id = 5;
        git.getFeed(id, new Callback<gitmodel>() {
            @Override
            public void success(gitmodel gitmodel, Response response) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getApplicationContext(), "Errors", Toast.LENGTH_LONG).show();
            }
        });
}

but no luck.

Please tell me where do I need to change to make it work. ?

Response I am getting in browser is,

<ArrayOfstudents xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/APICall.Controllers">
<students>
<studentAddress>valsad</studentAddress>
<studentId>1</studentId>
<studentName>Keval</studentName>
</students>
<students>
<studentAddress>Hyderabad</studentAddress>
<studentId>2</studentId>
<studentName>Honey</studentName>
</students>
</ArrayOfstudents>
Showthrough answered 25/7, 2015 at 7:0 Comment(10)
Your Android device won't be able to connect to localhost. You need to use an ip that's accessible over the LAN. Make sure you can access the address from the browser on your phone.Belike
Thanks for reply, Please suggest any article which tells about accessing localhost on device and other configurations.Showthrough
You are using 10.0.2.2. That will only work if your app runs on an emulator. You did not tell where your app runs on. A real device?Interferometer
Yes, I want to run app on real device. But on emulator too is giving error with message, failed to connect to /10.0.2.2 (port 52511) after 15000ms.Showthrough
For real device I am following this article "developer.chrome.com/devtools/docs/…", but no luckShowthrough
Working url in browser is, localhost:52511/api/Values/GetProduct/5 and my IPV4 address is : 196.168.1.2. So, I am using url : 192.168.1.2:52511/api/Values/GetProduct/5 But it shows bad request.Showthrough
You should try 192.168.1.2:52511 in a browser on your device.Interferometer
Bad Request - Invalid Hostname (400)Showthrough
You can try this once and check after firewall settings. (#45941361)Monoplane
Does this answer your question? How to connect to my http://localhost web server from Android EmulatorRomie
T
24

Instead of using 127.0.0.1:<PORT> use your local IP

Titanism answered 23/12, 2019 at 12:54 Comment(0)
V
6

There's nothing to do with retrofit library in your case.

It's about your network settings, your phone and IIS server must be the same LAN.

You can follow as below.

  1. Launch an AP on you IIS server;
  2. Connecting the AP with your phone.

Sometimes you need to close security firewall on your IIS server.

Vagary answered 25/7, 2015 at 7:23 Comment(2)
Still I am not done with this issue, but answer shows me correct way :)Showthrough
Working url in browser is, localhost:52511/api/Values/GetProduct/5 and my IPV4 address is : 196.168.1.2. So, I am using url : 192.168.1.2:52511/api/Values/GetProduct/5 But it shows bad request.Showthrough
S
5

I had the same problem as u (failed to connect to localhost/127.0.0.1). First of all i recommend u to read this: android developer's guide (main part of this on the image↓) enter image description here

I had all of this, mean A, B and C. As C(emulator) i used a Genymotion. And i solved my problem by changing Network Configurations on it.

IMPORTANT: my adress in baseURL were: "localhost:8099/"

Let's see it: U should click on SettingsNetwork → check Use HTTP Proxy → input your IP adress and Port(in my situation it was 8099) → close. See it more details on image ↓

enter image description here

enter image description here

Subplot answered 16/7, 2017 at 10:34 Comment(0)
C
0

I have the same problem, and I resolve it by changing the IP address instead of 'localhost':

  1. open cmd(if you are using windows): type 'ipconfig' and see your computer's ip.(make sure that your computer and your android device connect to the same network).
  2. remove 'localhost' in the url and use the ip(ip of your computer in step 1).
  3. run the project again and check
Cannonry answered 29/3, 2022 at 15:20 Comment(0)
S
0

In retrofit if you are trying to use a server from localhost you need to change in the base retrofit URL to the IP Local of your machine (from windows use the ipconfig command in cmd, and copy the IPV4 address), and you also need to add this to the AndroidManifest.xml:

   <application
    ...
    android:usesCleartextTraffic="true"
    ...
   </application>

Remember to use http or https in the URL as you configured

Retrofit.Builder().baseUrl("http://<YOUR_IP_Local>:<Port>")
Strontia answered 2/5 at 4:4 Comment(0)
H
-3

Actually this helped when using 2 apps on the same Android device (Android 9)

android:usesCleartextTraffic="true"
Hermetic answered 3/6, 2021 at 16:5 Comment(0)
M
-8

You can try this in androidmanifest.xml

android:usesCleartextTraffic="true"
Mendes answered 2/10, 2019 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.