嗨,我现在一直在努力解决这个问题,但找不到解决办法。
在java中,这段代码有效:
InetAddress serverAddr = InetAddress.getByName("myservice.local");
Socket socket = new Socket(serverAddr, PoolServerAddress.DEFAULT_PORT);
在android中同样不起作用。我尝试删除“.local”并在清单中添加所有必要的permsissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
我还尝试在执行该代码之前添加多播锁,但无法解决此问题。
我得到以下例外情况:
06-03 13:18:14.438: W/System.err(31636): java.net.UnknownHostException: Unable to
resolve host <server_name>: No address associated with hostname
06-03 13:18:14.438: W/System.err(31636): at
java.net.InetAddress.lookupHostByName(InetAddress.java:434)
06-03 13:18:14.438: W/System.err(31636): at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
06-03 13:18:14.438: W/System.err(31636): at
java.net.InetAddress.getByName(InetAddress.java:292)
06-03 13:18:14.438: W/System.err(31636): at
com.example.jelly_test.MainActivity.connect(MainActivity.java:113)
06-03 13:18:14.438: W/System.err(31636): at
com.example.jelly_test.MainActivity$ResolvingThread.run(MainActivity.java:71)
06-03 13:18:14.438: W/System.err(31636): Caused by: libcore.io.GaiException:
getaddrinfo
failed: EAI_NODATA (No address associated with hostname)
06-03 13:18:14.438: W/System.err(31636): at libcore.io.Posix.getaddrinfo(Native
Method)
06-03 13:18:14.438: W/System.err(31636): at
libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
06-03 13:18:14.438: W/System.err(31636): at
java.net.InetAddress.lookupHostByName(InetAddress.java:415)
06-03 13:18:14.438: W/System.err(31636): ... 4 mor
如果我提供ip而不是服务,它可以工作,但我需要能够提供名称。 此外,我已经尝试将JmDNS解析名称添加到ip中,但它很慢,只适用于办公室的内部服务,但我们的一些服务是在网络外部,但它们仍然可以使用java版本访问但不是android 。 还有什么我可以尝试的吗?非常感谢你的想法耗尽......!
编辑: 我认为我的问题是因为android中有一个bug,它只接受FQDN,例如: myservice.company.com
答案 0 :(得分:0)
根据我的要求,我发现临时解决这个问题的选项,即使在最新的Android版本(编写本文时为4.4),该错误仍未修复。
确保将问题中指示的所有必需权限添加到清单。
选项1: 如果您不知道这是一个好的教程,请使用JmDNS库来发现可用服务器列表并找到您的服务器: http://home.heeere.com/tech-androidjmdns.html 这个方法很慢,我发现有些服务没有显示(他们必须用zeroconf做广告)
选项2: 要求您的用户输入完全合格的域名,例如name.comany.com而不是name.local
选项3: 这真是一个黑客,但如果您知道FQDN的类型为someName.myComp.com并且用户输入了someName.local,则必须删除“.local”并尝试猜测该域名是什么:
private String getTargetIp(String host) {
System.out.println("Will try to create socket with "+host);
MulticastLock multicastLock = null;
String ipToReturn = null;
Socket socket = null;
try {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
multicastLock = wifiManager.createMulticastLock("any_name");
multicastLock.acquire();
DhcpInfo info = wifiManager.getDhcpInfo();
int ip = info.ipAddress;
InetAddress local = getInetAddress(ip);
String localHostName = local.getCanonicalHostName();
Log.d(TAG, "Device ip addr1 = "+local +" name= "+localHostName);
String[] temp = localHostName.split("\\.");
//replace the first part with our name and obtain FQDN
String targetFullHostName=localHostName.replace(temp[0], host);
Log.d(TAG, "targetFullHostName = "+targetFullHostName);
//this is not mandatory but here we are trying to retrieve the ip address
//it maybe also a way to check if we guessed right
InetAddress serverAddr = InetAddress
.getByName(targetFullHostName);
socket = new Socket(serverAddr,
PoolServerAddress.DEFAULT_PORT);
ipToReturn = socket.getInetAddress().getHostAddress();
Log.d(TAG, "socket remote address=" + ipToReturn);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
Log.e(TAG, "Other Exception");
}
finally {
if (multicastLock != null)
multicastLock.release();
if(socket!=null)
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ipToReturn;
}
选项4: 从api level 16开始,您可以使用Android Service Discovery侦听器列出可用服务并找到您要查找的服务。这与JmDNS的工作方式类似:您发现所有服务并尝试在列表中找到您的服务。以下是doc:http://developer.android.com/training/connect-devices-wirelessly/nsd.html#discover
的链接