isReachable无法在android studio中工作

时间:2015-05-26 05:49:40

标签: android xbmc

我正在使用XBMC android源代码,现在尝试将一些代码提取到新的xbmc android项目中。当我尝试连接到IP地址并检查是否可以使用isReachable()访问时,它会抛出一个NetworkOnMainThreadException但是相同的源代码(在我从git下载的XBMC源代码中)它工作正常, 这是我正在处理的源代码

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" />

我不知道,问题是什么,我该如何解决?

1 个答案:

答案 0 :(得分:2)

NetworkOnMainThreadException exception表示您在Networking Work上执行了一些MainThread(这将超过Thread并抛出Exception)。

因此,您必须将Networking Code移至Background Thread(另一个thread,然后移至Main Thread)。

AsyncTask  是处理此类事情的一个很好的例子