commit 766c9eed0b89d94f8221c868a3fdb58a27d9f0c4
parent 1c7d0c59cb9310dd1423b6107cabeb49659f9ced
Author: Bertrand BRUN <bertrand0brun@gmail.com>
Date: Tue, 1 Feb 2011 16:11:24 +0100
Correction dans la classe Network pour prendre en compte l'authentification
Diffstat:
4 files changed, 28 insertions(+), 10 deletions(-)
diff --git a/code/PtiClic/src/org/pticlic/games/BaseGame.java b/code/PtiClic/src/org/pticlic/games/BaseGame.java
@@ -36,8 +36,10 @@ public class BaseGame extends Activity implements OnClickListener {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String serverURL = sp.getString(Constant.SERVER_URL, "http://dumbs.fr/~bbrun/pticlic.json"); // TODO : Mettre comme valeur par defaut l'adresse reel du serveur
-
- Network network = new Network(serverURL, Mode.SIMPLE_GAME);
+ String id = sp.getString(Constant.USER_ID, "joueur");
+ String passwd = sp.getString(Constant.USER_PASSWD, "");
+
+ Network network = new Network(serverURL, Mode.SIMPLE_GAME, id, passwd);
game = network.getGames(1);
int nbrel = game.getNbRelation();
nbWord = game.getNbWord();
diff --git a/code/PtiClic/src/org/pticlic/model/Constant.java b/code/PtiClic/src/org/pticlic/model/Constant.java
@@ -3,6 +3,10 @@ package org.pticlic.model;
public class Constant {
public static final String SERVER_URL = "SERVER_URL";
+ // Constant pour les information de l'utilisateur.
+ public static final String USER_ID = "login";
+ public static final String USER_PASSWD = "passwd";
+
// Constant pour les intents
public static final String SCORE_INTENT = "SCORE_INTENT";
}
diff --git a/code/PtiClic/src/org/pticlic/model/Game.java b/code/PtiClic/src/org/pticlic/model/Game.java
@@ -1,7 +1,6 @@
package org.pticlic.model;
import java.io.Serializable;
-import java.util.ArrayList;
public class Game implements Serializable {
diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java
@@ -36,16 +36,22 @@ public class Network {
private Mode mode;
private String serverURL;
+ private String id;
+ private String passwd;
/**
* Constructeur
*
* @param serverURL Chaine de caractères représentant l'URL où se situe le serveur.
* @param mode Le type de partie que l'on veut récupérer.
+ * @param id L'indentifiant du joueur.
+ * @param passwd Le mot de passe de l'utilisateur.
*/
- public Network(String serverURL, Mode mode) {
+ public Network(String serverURL, Mode mode, String id, String passwd) {
this.mode = mode;
this.serverURL = serverURL;
+ this.id = id;
+ this.passwd = passwd;
}
/**
@@ -59,6 +65,8 @@ public class Network {
URL url = new URL(this.serverURL);
URLConnection connection = url.openConnection();
connection.addRequestProperty("action", "getparties");
+ connection.addRequestProperty("user", this.id);
+ connection.addRequestProperty("passwd", this.passwd);
connection.addRequestProperty("nb", String.valueOf(nbGames));
connection.addRequestProperty("mode", mode.value());
@@ -128,25 +136,30 @@ public class Network {
* Cette méthode permet d'envoyer les parties au serveur pour qu'il puisse les
* rajouter à la base de données, et calculer le score.
* @param game La partie jouee par l'utilisateur
- * @return <code>true</code> si on a pu envoyer la partie au serveur <code>false</code> sinon.
+ * @return Le score sous forme JSON.
*/
- public boolean sendGame(GamePlayed game) {
+ public String sendGame(GamePlayed game) {
+ String score = null;
try {
URL url = new URL(this.serverURL);
URLConnection connection = url.openConnection();
connection.addRequestProperty("action", "sendpartie");
+ connection.addRequestProperty("user", this.id);
+ connection.addRequestProperty("passwd", this.passwd);
connection.addRequestProperty("mode", mode.value());
Gson gson = new Gson();
String json = gson.toJson(game);
connection.addRequestProperty("json", json);
+ JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
+
+ // TODO : A changer lorsque je serais exactement ce que renvoie le serveur.
+ score = gson.fromJson(reader, String.class);
- // TODO : Regarder si il ne faudrait pas que le serveur renvoie une valeur indiquant si l'action c'est bien derouler.
- connection.connect();
} catch (IOException e) {
- return false;
+ return score;
}
- return true;
+ return score;
}
}