【问题标题】:android 4 emulator cannot connect with localhostandroid 4模拟器无法连接本地主机
【发布时间】:2013-07-14 17:27:46
【问题描述】:

我想连接我的 web API,它的 URL 是 http://localhost:55154/,带有 android 模拟器。

AsyncHttpClient client = new AsyncHttpClient();

client.get("http://10.0.2.2:55154/api/values", new AsyncHttpResponseHandler() {    
    @Override
    public void onSuccess(String response) {
        System.out.println(response);
    }
});

但是,当我连接到 google.pl 时,我没有收到任何回复。
我用 localhost 地址尝试了多种变体,但没有一个有效。

我将这个库用于我的异步连接: http://loopj.com/android-async-http/

更新:

当我运行我的程序时,我从 LogCat 收到以下错误

07-17 08:58:26.495: E/Trace(879): error opening trace file: No such file or directory (2)

但是我可以通过浏览器与http://localhost:55154/api/values 联系。

【问题讨论】:

    标签: android api localhost emulation loopj


    【解决方案1】:

    使用 IP 地址10.0.2.2 而不是10.0.0.2

    参考http://developer.android.com/tools/devices/emulator.html#networkaddresses

    【讨论】:

    • 我之前有 10.0.2.2,我发布了错误的代码,因为我多次更改它。还是谢谢。
    【解决方案2】:

    像这样使用http://10.0.2.2:55154/ 来连接你的端口地址

    【讨论】:

      猜你喜欢
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2019-12-23
      • 2012-03-22
      相关资源
      最近更新 更多