GalloPinto
GalloPinto

Reputation: 677

Trying to access localhost from a Java restAdapter on Android

I'm making an android application wich uses a web service. To access to it, we use a restAdapter. This is the code:

private String URL_PLACE = "http://myaddresstomywebservice";

public RouteCLlegar(Context ctx) {
    restAdapter = new RestAdapter.Builder()
            .setEndpoint(URL_PLACE)
            .build();
    cLlegar = restAdapter.create(CLlegar.class);
}

When we access the web service hosted on the web, it works, but when we use localhost, it doesn't.

private String URL_PLACE = "http://localhost:53285";

public RouteCLlegar(Context ctx) {
    restAdapter = new RestAdapter.Builder()
            .setEndpoint(URL_PLACE)
            .build();
    cLlegar = restAdapter.create(CLlegar.class);
}

It gives this error:

24963-25327/ni.femer.busesmg.app I/ERROR﹕ el error java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 53285) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused

enter image description here

How can i access localhost from my rest adapter?

Upvotes: 1

Views: 260

Answers (2)

user1704369
user1704369

Reputation: 153

I would suspect, localhost is your Android device. If you realy want to access something on localhost, you would have to deploy a http Server in your Android device.

Upvotes: 1

makovkastar
makovkastar

Reputation: 5020

  1. You can run your application on an Android emulator and use 10.0.2.2 instead of localhost.
  2. You can use a local IP address of your PC (check it with ipconfig, should be something like 192.168.1.x), if it's on the same wireless network with your Android device.

Don't forget to specify also a port number.

Upvotes: 1

Related Questions