简体   繁体   中英

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

在此处输入图片说明

How can i access localhost from my rest adapter?

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM