Hi!
I was trying to figure out how to check if a device is connected to the internet, but couldn’t find any helpful results on google (this is the only thing I found). I eventually figured it out, and wanted to share my code, so that other beginners like me won’t have to google for hours. I’ve seen other people post stuff like this, so I hope this is not considered as a waste of space or anything.
boolean isConnected() {
String[] file; //creates a String that will be loaded with stuff from the internet
try {
//load a text file from somewhere on the internet
file = loadStrings("YourURLWithAFileExtensionLike.txtAtTheEnd");
//TRY to print it --> throws NullPointerException if it was not loaded correctly
println(file[0]);
}
catch (NullPointerException e) { //if there is an exception, do this:
println("Connection is not available...");
return false; //function returns false --> not connected
}
return true; //function returns true --> connected; I can put this here,
//because the "return" earlier immediately exits the function,
//so that this will never be called, unless there is a connection
}