[英]Trying to access localhost from a Java restAdapter on Android
I'm making an android application wich uses a web service. 我正在做一个使用网络服务的android应用程序。 To access to it, we use a restAdapter. 要访问它,我们使用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. 当我们访问托管在Web上的Web服务时,它可以工作,但是当使用localhost时,它就不能。
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 24963-25327 / ni.femer.busesmg.app I / ERROR:错误java.net.ConnectException:15000毫秒后无法连接到localhost / 127.0.0.1(端口53285):isConnected失败:ECONNREFUSED(连接被拒绝
How can i access localhost from my rest adapter? 我如何从我的rest适配器访问localhost?
I would suspect, localhost is your Android device. 我怀疑,本地主机是您的Android设备。 If you realy want to access something on localhost, you would have to deploy a http Server in your Android device. 如果您确实想访问localhost上的内容,则必须在Android设备中部署http服务器。
10.0.2.2
instead of localhost
. 您可以在Android模拟器上运行应用程序,并使用10.0.2.2
代替localhost
。 ipconfig
, should be something like 192.168.1.x
), if it's on the same wireless network with your Android device. 如果您的PC的本地IP地址与Android设备位于同一无线网络中,则可以使用它的本地IP地址(通过ipconfig
检查,应该类似于192.168.1.x
)。 Don't forget to specify also a port number. 不要忘记还要指定一个端口号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.