无法使用http://10.0.2.2在模拟器上使用Retrofit

时间:2014-07-08 22:08:50

标签: android android-emulator localhost retrofit

我最近下载了Retrofit 1.6.1并尝试在简单示例上进行测试。 我注意到它不能使用10.0.2.2地址通过模拟器工作。

这是我简单的Java代码:

RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://10.0.2.2/sisecurite").build();
IssueTicketInterface issueTickets = restAdapter.create(IssueTicketInterface.class);

issueTickets.syncIssueTickets(55, new Callback<IssueTicketsCollection>() {
    @Override
    public void success(IssueTicketsCollection issueTickets, Response response) {
        System.out.println("Response : " + response);
        // TODO
    }

    @Override
    public void failure(RetrofitError exception) {
        Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
    }
});

Retrofit给出的错误是:

failed to connect to localhost/127.0.0.1 (port 80) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)

错误直接显示,它不会等待15秒,如错误消息中所示。此外,10.0.2.2似乎转换为127.0.0.1

以下是failure()方法的完整堆栈跟踪:

retrofit.RetrofitError: failed to connect to localhost/127.0.0.1 (port 80) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:389)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:223)
at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:843)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
at com.android.okhttp.Connection.connect(Connection.java:101)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
... 7 more
Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:208)
... 23 more

请注意,我可以通过模拟器的网络浏览器完全访问http://10.0.2.2/sisecurite,因此它似乎是特定于Retrofit的错误。

有人总是有同样的问题,或者我错过了什么?

1 个答案:

答案 0 :(得分:0)

我终于找到了这种行为的原因。

原来,我尝试访问重定向我的页面到登录表单。这就是为什么Retrofit抛出一个错误,说“无法连接到localhost / 127.0.0.1”。