com.stealthcopter.networktools NetworkOnMainThreadException

 com.stealthcopter.networktools NetworkOnMainThreadException
            
android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1668)
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
        at java.net.InetAddress.getByName(InetAddress.java:1106)
        at com.stealthcopter.networktools.PortScan.onAddress(PortScan.java:61)

      
String ipText = "111.111.111.111"            
    PortScan
    .onAddress(ipText)//<-------------- CRASH NetworkOnMainThreadException
           .setTimeOutMillis(time)
           .setPorts(ports)
           .setMethodTCP()
           .doScan(portListener);
           
           
           
           
    Replace
    
    //This i new Thread
    InetAddress aa =InetAddress.getByName(ipText);
    
//And Callback to this
   PortScan.onAddress(aa)
           .setTimeOutMillis(time)
           .setPorts(ports)
           .setMethodTCP()
           .doScan(portListener);

 

 

 

 //onCreate

executor = Executors.newSingleThreadExecutor();
handler = new Handler(Looper.getMainLooper());

 

 

executor.execute(() -> {
//Background work here
try {
InetAddress aa = InetAddress.getByName(ipText);
handler.post(() -> {
//UI Thread work here
PortScan.onAddress(aa)
.setTimeOutMillis(time)
.setPorts(ports)
.setMethodTCP()
.doScan(portListener);
});
} catch (UnknownHostException e) {
e.printStackTrace();
}

});
 

 

Комментариев нет:

Отправить комментарий