Keval Patel
Keval Patel

Reputation: 995

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>

Upvotes: 21

Views: 67735

Answers (7)

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>")

Upvotes: 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

Upvotes: 0

Dmitry Konyshev
Dmitry Konyshev

Reputation: 7

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

android:usesCleartextTraffic="true"

Upvotes: -3

ihaydinn
ihaydinn

Reputation: 197

You can try this in androidmanifest.xml

android:usesCleartextTraffic="true"

Upvotes: -8

Dewsworld
Dewsworld

Reputation: 14033

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

Upvotes: 24

ahuminskyi
ahuminskyi

Reputation: 228

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

Upvotes: 5

Xiaozou
Xiaozou

Reputation: 1665

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.

Upvotes: 6

Related Questions