Java Check URL connection

I have recently created a versionControl in my Calculator pulling a version file from a URL and it works great the example can be found here .

Well that is great but when I was running some tests I noticed if there was no internet connection or there was no version file available it had exception errors (expected) so I created a little function to check if the URL exist before I try to use it, when if  we can connect to it, if so then get the version file.  Here is the sip of code that made that possible.

———– CODE SNIP ————-

boolean connectivity;
try
{
URL version = new URL("http://localhost/version.txt");
URLConnection conn = version.openConnection();
conn.setConnectTimeout(5000);
conn.connect();
connectivity = true;
System.out.println("ALL GOOD / " + connectivity);
}
catch (Exception e)
{
connectivity = false;
System.out.println("NO GOOD / " + connectivity);
}

But if you want the full code take a look bellow…. BUT remember to replace the URL with your own URL or location of the version file, you can also change what to do if the connection exist or not exist and if the version is the same or different but that is up to you.

This is what you will see if you application version is lower than the version saved in the version.txt file

new-sersion

———– START CODE ————-

package test;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JOptionPane;

public class versionControl {
public static void main(String[] args) throws Exception {

boolean connectivity;

try
{
URL version = new URL(“http://localhost/version.txt”);
URLConnection conn = version.openConnection();
conn.setConnectTimeout(5000);
conn.connect();
connectivity = true;
System.out.println(“can connect / ” + connectivity);

BufferedReader in = new BufferedReader(new InputStreamReader(version.openStream()));

String newVersion = in.readLine();
String currentVersion = “1.0.2”;

String currentValue = currentVersion.replaceAll(“\\.”, “”);
String newValue = newVersion.replaceAll(“\\.”, “”);

int x = Integer.parseInt(currentValue);
int y = Integer.parseInt(newValue);

if (x == y){
JOptionPane.showMessageDialog(null, “Your Version is up to date!”);
} else if (x > y){
System.out.println(“Contact Developer Something has gone wrong….”);
} else if (x < y){
int selectedOption = JOptionPane.showConfirmDialog(null,
“Your Version is out of date! \n Download new Version?”,
“Update Available”,
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
System.out.println(“Starting to Download”);
try
{
Desktop.getDesktop().browse(new URL(“http://www.gcclinux.co.uk/?page_id=74”).toURI());
}
catch (Exception e) {}

} else {
System.out.println(“Don’t want new version”);
System.exit(0);
}
}
}
catch (Exception e)
{
connectivity = false;
System.out.println(“NO GOOD / ” + connectivity);
}
}

}

———– END CODE ————-

Enjoy it and I hope you find it useful

btn_donate

Leave a Reply