我正在使用此方法获取BroadCast地址数组。
旧API中不存在使用的某些方法和类 所以我在方法的开头添加了一个版本检查。我正在尝试在Android 1.6模拟器上运行它,它失败并出现验证错误。
private InetAddress[] getBroadCast() throws SocketException
{
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < 9)
{
try {
String ipString = getWifiIp();
int loc = ipString.lastIndexOf(".");
InetAddress baddrs[] = new InetAddress[1];
baddrs[0] = new InetAddress.getByName(ipString.substring(0, loc)+".255");
return baddrs;
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}
else{
String bcast="";
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (InterfaceAddress address : intf.getInterfaceAddresses()) {
if(address.getBroadcast()!=null) bcast=bcast+address.getBroadcast().toString().substring(1)+"::";
}}
String[] bcastarray= bcast.split("::");
InetAddress baddrs[] = new InetAddress[bcastarray.length];
for(int i=0;i<bcastarray.length;i++)
try {
baddrs[i] = new InetAddress.getByName(bcastarray[i]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return baddrs;
}
}
这些是出现的错误
10-12 11:38:22.030: W/dalvikvm(297): VFY: unable to resolve virtual method 4463: Ljava/net/NetworkInterface;.getInterfaceAddresses ()Ljava/util/List;
10-12 11:38:22.030: W/dalvikvm(297): VFY: rejecting opcode 0x6e at 0x0071
10-12 11:38:22.040: W/dalvikvm(297): VFY: rejected Lhello/BroadcastSend;.getBroadCast ()[Lhello/IPMAddress;
10-12 11:38:22.040: W/dalvikvm(297): Verifier rejected class Lhello/BroadcastSend;
10-12 11:38:22.040: W/dalvikvm(297): threadid=19: thread exiting with uncaught exception (group=0x4001aa28)
android.os.Build.VERSION.SDK_INT仅适用于API Level 4及更高版本。我正在寻找解决这些问题的方法
答案 0 :(得分:0)
这不适用于1.6。 1.6类加载器将尝试检查所有方法,即使您实际上没有调用它们,这就是您收到错误的原因。您必须创建一个调用getInterfaceAddress()
的包装类,以便它根本不会被加载。