我无法从桌面上的程序访问模拟器上的 Android 服务器,我该如何解决?
一些代码(来自 How to find LAN ip address of android device? ):
public static ArrayList<String> getSelfIP(){
try {
ArrayList<String> ipList = new ArrayList<>();
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ipList.add(inetAddress.getHostAddress().toString());
}
}
}
return ipList;
} catch (SocketException ex) {}
return null;
}
结果是[fe80::5054:ff:fe12:3456%eth0, 10.0.2.15]
我必须配置什么或做什么才能使我的桌面程序可以访问模拟器?
我做了以下事情:
> adb forward tcp:50000 tcp:50000
但是,我无法通过 localhost:50000 访问服务器。
最佳答案
尝试使用 IP 地址 10.0.2.2。
它是您的主机环回接口(interface)的特殊别名(即您开发机器上的 127.0.0.1)
关于安卓服务器套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30933735/