http://10.0.2.2在Android模拟器的作品,但不是作为一个模拟器Android设备 [英] http://10.0.2.2 works on the android emulator but not on android device used as a emulator

查看:704
本文介绍了http://10.0.2.2在Android模拟器的作品,但不是作为一个模拟器Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过USB连接Android设备使用它作为一个模拟器,如果我访问的日食模拟器它的工作原理,但同样的网址,如果我从设备访问的模拟器它使连接超时错误。

I connected my android device via USB to use it as a emulator, if I access the URL from eclipse emulator it works but the same if I access from device as emulator it gives connection time out error.

有没有设置,我需要改变吗?还是对此有任何可能的解决方案?

is there any settings do I need to change for this? or is there any possible solution for this?

感谢

推荐答案

这是我必须做的,以获得该设备搭载了本地服务实例的本地主机上我的Windows PC上运行。

Here is what I had to do in order to get the device to pick up a local service instance on my windows PC running on localhost.

  • 打开无线设备上,并连接到无线网络。
  • 在命令提示符下运行IPCONFIG。
  • 使用的IPv4地址我的以太网适配器局域网连接(因为我通过以太网连接)
  • 更改基本URL在android应用程序中使用该地址
  • Turn on WiFi on my device and connect to the wireless network.
  • Run ipconfig in the command prompt.
  • Use the IPv4 Address for my Ethernet Adapter Local Area Connection (since I'm connected via ethernet)
  • Change the base url in the android application to use that address

然后我的Andr​​oid应用程序能够通过该IP地址连接到本地服务实例,而不是10.0.2.2,当你运行模拟器上的应用程序,它的工作原理。如果一个人是这样的倾向,你可以提取这些URL,并检查应用程序是否在模拟器或设备上运行,然后在code适当地设定IP地址。希望这有助于。

Then my android application was able to connect to the local service instance via that IP address rather than 10.0.2.2, which works when you're running the application on the emulator. If one was so inclined, you could extract those urls and check whether the application was running on the emulator or the device, and then set the ip address appropriately in code. Hope this helps.

编辑对于上述网址萃取 - 我创建了一个ApplicationName.java文件,并声明该变量:

Edit in regard to above url extraction - I created an ApplicationName.java file and declared this variable:

public static String ANDROID_DEVICE_ID = "";

然后设置该变量在我最初的活动:

Then set that variable in my initial activity:

ApplicationName.ANDROID_DEVICE_ID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

然后我复制粘贴的设备ID的,在我的API类,使Web服务调用,我有以下code:

Then I copy pasted that device Id, and in my api class that makes the web service calls I have the following code:

private String getCategoriesUrl() {
    // TODO: Remove before production
    if (AgoraApplication.ANDROID_DEVICE_ID.equals("deviceIdString")) {
        _categoriesUrl = _deviceIp + _categoriesUrlSuffix;
    }
    else {
        _categoriesUrl = _emulatorIp + _categoriesUrlSuffix;
    }
    return _categoriesUrl;
}

所以,当我在任何一台设备或模拟器调试在我的应用程序将自动使用相应的IP地址,服务电话。

So when I'm debugging on either device or emulator my application automatically uses the appropriate IP address for service calls.

这篇关于http://10.0.2.2在Android模拟器的作品,但不是作为一个模拟器Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆