diff --git a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java index 107419967..74d8bd479 100644 --- a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java +++ b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java @@ -52,11 +52,31 @@ protected boolean isUsableAddress(NetworkInterface networkInterface, InetAddress // TODO: Workaround Android DNS reverse lookup issue, still a problem on ICS+? // http://4thline.org/projects/mailinglists.html#nabble-td3011461 String hostName = address.getHostAddress(); - try { - Field field = InetAddress.class.getDeclaredField("hostName"); - field.setAccessible(true); - field.set(address, hostName); - } catch (Exception ex) { + + Field field0 = null; + Object target = null; + + try { + + try { + field0 = InetAddress.class.getDeclaredField("holder"); + field0.setAccessible(true); + target = field0.get(address); + field0 = target.getClass().getDeclaredField("hostName"); + } catch( NoSuchFieldException e ) { + // Let's try the non-OpenJDK variant + field0 = InetAddress.class.getDeclaredField("hostName"); + target = address; + } + + if (field0 != null && target != null && hostName != null) { + field0.setAccessible(true); + field0.set(target, hostName); + } else { + return false; + } + + } catch (Exception ex) { log.log(Level.SEVERE, "Failed injecting hostName to work around Android InetAddress DNS bug: " + address, ex @@ -90,4 +110,4 @@ protected void discoverNetworkInterfaces() throws InitializationException { super.discoverNetworkInterfaces(); } } -} \ No newline at end of file +}