-1
我使用XBMC android源代碼,現在嘗試提取一些代碼到新的xbmc android項目。當我嘗試連接到一個IP地址,並檢查它是否可以使用isReachable(),它是拋出一個NetworkOnMainThreadException但相同的源代碼(在XBMC源代碼,我從git downloded)它工作正常, 這是源代碼我工作isReachable在android工作室不工作
Handler handler = new Handler(){
public void handleMessage(android.os.Message message){
if(message.getData().containsKey(MacAddressResolver.MESSAGE_MAC_ADDRESS)){
String mac = message.getData().getString(MacAddressResolver.MESSAGE_MAC_ADDRESS);
if(!mac.equals("")) {
mMacAddrView.setText(mac);
Toast toast = Toast.makeText(getContext(), "Updated MAC for host: " + mHostView.getText().toString() + "\nto: " + mac, Toast.LENGTH_SHORT);
toast.show();
}
}
}
};
public class MacAddressResolver implements Runnable{
private String mHost = null;
private String mMac = null;
private Handler mHandler;
public static final String MESSAGE_MAC_ADDRESS = "MAC_ADDRESS";
public MacAddressResolver(String ipString, Handler handler){
mHost = ipString;
mHandler = handler;
}
public void run(){
mMac = arpResolve(mHost);
Bundle bundle = new Bundle();
bundle.putString(MESSAGE_MAC_ADDRESS, mMac);
Message message = new Message();
message.setData(bundle);
mHandler.sendMessage(message);
}
private String arpResolve(String host){
System.out.println("ARPRESOLVE HOST: " + host);
try{
//Parse it as a proper InetAddress - it might be a hostname. We don't know yet.
InetAddress inet = InetAddress.getByName(host);
//initiate some sort of traffic to ensure we get an arp entry (if we're on same subnet, that is...)
inet.isReachable(500); //timeout of 500ms. just to trigger the arp resolution process
//Get the official string representation of the resolved ip address
String ipString = inet.getHostAddress();
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line = "";
while(true){
line = br.readLine();
if (line == null)
break;
if(line.startsWith(ipString)){
br.close();
System.out.println("ARPRESOLVE MAC:\n" + line);
return line.split("\\s+")[3]; // 4th word, separated by "whitespace"
}
}
br.close();
return "";
}catch(Exception e){
return "";
}
}
}
,我也把這兩個權限在我的AndroidManifest.xml文件
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="13" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我不知道,是什麼問題我該如何解決它?