failed to connect to localhost/127.0.0.1 android

43,939

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
43,939

Related videos on Youtube

Keval Patel
Author by

Keval Patel

Updated on July 09, 2022

Comments

  • Keval Patel
    Keval Patel 6 months

    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>
    
    • Will Calderwood
      Will Calderwood over 7 years
      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.
    • Keval Patel
      Keval Patel
      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.
  • Keval Patel
    Keval Patel over 7 years
    Still I am not done with this issue, but answer shows me correct way :)
  • Keval Patel
    Keval Patel over 7 years
    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.

Related