解决android网络异常java.net.SocketException: Bad address family


在Android系统中进行网络开发时,如果使用了java.nio等相关包后有可能碰到异常java.net.SocketException: Bad address family。原因是Android2.2bug不能启用IPV6协议,具体可点击这里。
异常信息:

java.net.SocketException: Bad address family
     at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
     at org.apache.harmony.nio.internal.SocketChannelImpl.connect(SocketChannelImpl.java:272)
     at org.apache.harmony.nio.internal.PipeImpl$SinkChannelImpl.finishConnect(PipeImpl.java:164)
     at org.apache.harmony.nio.internal.PipeImpl.<init>(PipeImpl.java:48)
     at org.apache.harmony.nio.internal.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:51)
     at org.apache.harmony.nio.internal.SelectorImpl.</init><init>(SelectorImpl.java:141)
     at org.apache.harmony.nio.internal.SelectorProviderImpl.openSelector(SelectorProviderImpl.java:58)</init>

解决代码:
static {
java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
}
转载: http://orgcent.com/android-bad-address-family-exception/ | 萝卜白菜的博客

你可能感兴趣的:(exception)