Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed to connect to localhost/127.0.0.1 android

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>
like image 739
Keval Patel Avatar asked Jul 25 '15 07:07

Keval Patel


People also ask

What is localhost address on Android?

You can access your host machine with the IP address "10.0. 2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".

Why my localhost can t be reached?

It is triggered if the firewall wrongly blocks your server or you're using the wrong port. The localhost error can also happen if your Apache web server or Chrome browser is not configured correctly.

Could not connect to 127. 0 0. 1 Connection Refused?

0.1 (loopback address). So your client is trying to connect to any of the non-loopback addresses of your machine, while your server is listening only on the loopback address . So, no connection can be established. The solution to this problem is that connect to the same end point your server is listening on.


1 Answers

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.

like image 188
Xiaozou Avatar answered Sep 20 '22 22:09

Xiaozou