Okay, so I’ve developed a simple tetris clone applet that I want to send score and user information to a PHP script after the game is over.
The PHP form then takes this information and submits it to a MySQL database (JDBC is not an option, I do not have remote access to the web server)
For some reason though, it doesn’t seem to work. If I manually execute the PHP file, it works fine (it creates a log of all scores submitted so I can debug this), but when I try to use the Java applet it seems to connect but it does not do anything afterwards. My code looks like this:
public void sendScores() {
String[] stats;
stats = Tetris.getGameStats();
sentScores = true;
URL url;
URLConnection urlConn;
DataOutputStream printout;
try {
String q = URLEncoder.encode("user_id", "UTF-8") + "=" + URLEncoder.encode(strUserId, "UTF-8");
q += "&" + URLEncoder.encode("ruleset", "UTF-8") + "=" + URLEncoder.encode(strRuleSet, "UTF-8");
q += "&" + URLEncoder.encode("gametype", "UTF-8") + "=" + URLEncoder.encode(strGameType, "UTF-8");
q += "&" + URLEncoder.encode("score", "UTF-8") + "=" + URLEncoder.encode(stats[0], "UTF-8");
q += "&" + URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(stats[1], "UTF-8");
q += "&" + URLEncoder.encode("timelimit", "UTF-8") + "=" + URLEncoder.encode(strTimeLimit, "UTF-8");
q += "&" + URLEncoder.encode("ruleset", "UTF-8") + "=" + URLEncoder.encode(strScoreLimit, "UTF-8");
System.out.println(q);
url = new URL (getCodeBase().toString() + "submitScore.php");
System.out.println(url.toString());
urlConn = url.openConnection();
urlConn.setDoOutput (true);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
printout = new DataOutputStream (urlConn.getOutputStream ());
printout.writeBytes(q);
printout.flush();
printout.close();
System.out.println("sendScore() executed");
} catch (Exception e) { }
}
As you can see I followed Java World Tip #34 very closely.
The applet does NOT throw an exception, so it appears to have executed all the code correctly, but it doesn’t appear to actually send the information to the script. There is very little out there beyond the javaworld site. If anyone wants to look at the php file I’ve attached it as well. It is supposed to create a log file even if no data is actually posted to the php script.