Correction dans la classe Network pour prendre en compte l'authentification
This commit is contained in:
parent
1c7d0c59cb
commit
766c9eed0b
|
@ -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();
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.pticlic.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class Game implements Serializable {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user