I've Been creating an app about checking the ip of x websites and I've imported:java.net.InetAddress and It will work on intelij but on android studio the try/catch block catches some errors: android.os.NetworkOnMainThreadException. So this the code part:
AsyncTask.execute(new Runnable() {
@Override
public void run() {
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InetAddress address = null;
try {
address = InetAddress.getByName(String.valueOf(hostnumber));
} catch (UnknownHostException e) {
e.printStackTrace();
}catch (Exception e) {
Toast.makeText(Lookup.this,"Problem"+e, Toast.LENGTH_SHORT).show();
}
output.setText((CharSequence) address);
}
});
}
});
I've added the internet permission on manifest: <uses-permission android:name="android.permission.INTERNET" />
The try/catch block caught: 'android.os.NetworkOnMainThreadException'
I saw some answers from stack such as: How to fix 'android.os.NetworkOnMainThreadException'? , https://www.tutorialspoint.com/how-to-fix-android-os-networkonmainthreadexception
....
BUT still it wouldn't show the IPs of the websites and show the error again. If you have any solution to this problem please lmk. Thanks In Advance!
UPDATE: I tried to put a boolean statement but it made it even worse throwing some more errors. Is maybe the wrong format to syntax these blocks? where should I fix the code?