My Java code produces link like this:
addscore.php&name=Namedd&score=0&hash=b61249d0207e4734879578733a86c3d6
A PHP file(addscore.php) should work with these informations and insert it into database with an sql query.
addscore.php:
<?php
$db = mysql_connect("host", "root", "password") or die('Could not connect: ' . mysql_error());
mysql_select_db("db1") or die('Could not select database');
$score = (int)$_GET['score'];
$hash = $_GET['hash'];
$name = mysql_real_escape_string($_GET['name'], $db);
$timestamp = date("Y-m-d H:i:s");
$secretKey="mySecretKey";
$real_hash = md5($score . $hash . $secretKey);
if($real_hash == $hash) {
$query = "INSERT INTO highscores (id, date, score, name) VALUES (NULL, '$timestamp', '$score', '$name')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
?>
Java code gives an error message, could not submit score to server. If I try to open given url(see above) I get a Not found
error.
UPDATE: Java code:
String hash = "";
String name = nameText.getText();
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update((name + score + HASH_SALT).getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
hash = sb.toString();
} catch (Exception e) {
hash = "";
}
try {
String urlParameters = PARAM_NAME + name + PARAM_SCORE + score + PARAM_HASH + hash;
URL url = new URL(HIGHSCORES_ADD + urlParameters);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.getInputStream();
urlConnection.disconnect();
} catch (Exception e){
Gdx.app.log( "HighScores", "Could not submit score" + e.getMessage());
} finally {
((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
}
Your Address should be
addscore.php?name=Namedd&score=0&hash=b61249d0207e4734879578733a86c3d6
note to first &
in the URL
In your Java code make sure to have ?
between HIGHSCORES_ADD
and urlParameters
when you fixed that if still have error in save data A good Idea is save your $query in a text or log file and try to check query in phpmyadmin
Another Thing you must note is Your insert is via php not with java ! update your question subject
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.