Reputation: 33
Okay, I've been trying to figure this out for a few hours and it's starting to kill me.
I wrote a primitive version checker for an app I work on every once and awhile. It's just a simple for fun project.
The version checker has been a pain though. It was working the first few times I tested it, but since then it was been returning the same value over and over, despite the file it was reading from having been deleted. I can only assume this means it was cached somewhere, and caching a version file kind of ruins the point of having it.
Here's the code I use for checking the version number from my website:
public int VersionNumber = 2005001; //to be updated to 2999999 when building to distribute
public static boolean CheckUpdate() throws Exception {
int ver = Integer.parseInt(getText("http://www.fragbashers.net/smite/version.txt"));
System.out.println(ver);
if (ver > VersionNumber) {
System.out.println("Current version lower than newest!");
return true;
} else {
return false;
}
}
public static String getText(String url) throws Exception {
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
return response.toString();
}
I use
int ver = Integer.parseInt(getText("http://www.fragbashers.net/smite/version.txt"));
URL newClient = new URL("http://www.fragbashers.net/smite/RGP_" + ver + ".jar");
and some JFileChooser code to download the new version. That causes no issues.
The file on my website has a 7 digit long int on it (2999999) that is the newest version file. Eclipse is printing out the old version (2005001).
Basically I need help figuring out why the file is being cached and how I can stop it from being cached so it always has the most up to date version.
Upvotes: 3
Views: 44
Reputation: 14471
Use URLConnection.setUseCaches(boolean);
.
In your case, it would be connection.setUseCaches(false);
Upvotes: 1