www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit d1a1fe7c19a4bf8f7f6a543286e3abc459557f76
parent e0d33694a005774f66546266045867fdc9cf6f70
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date:   Sat, 12 Feb 2011 15:29:04 +0100

Merge branch 'master' of github:jsmaniac/2011-m1s2-ter

Diffstat:
Mcode/PtiClic/AndroidManifest.xml | 2+-
Dcode/PtiClic/src/exception/PtiClicException.java | 47-----------------------------------------------
Mcode/PtiClic/src/org/pticlic/Score.java | 25+++++++++++++++++++++++--
Acode/PtiClic/src/org/pticlic/exception/PtiClicException.java | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mcode/PtiClic/src/org/pticlic/games/BaseGame.java | 3+--
Mcode/PtiClic/src/org/pticlic/model/Network.java | 58+++++++++++++++++++++++++++++++++++++++-------------------
6 files changed, 115 insertions(+), 71 deletions(-)

diff --git a/code/PtiClic/AndroidManifest.xml b/code/PtiClic/AndroidManifest.xml @@ -14,7 +14,7 @@ <activity android:name=".games.BaseGame" android:screenOrientation="portrait"></activity> <activity android:label="Information" android:name=".Information" android:screenOrientation="portrait"></activity> <activity android:name=".Score" android:label="Score" android:screenOrientation="portrait"></activity> -<activity android:name=".FrontPage" android:screenOrientation="portrait"></activity> +<activity android:name="FrontPage" android:screenOrientation="portrait"></activity> </application> diff --git a/code/PtiClic/src/exception/PtiClicException.java b/code/PtiClic/src/exception/PtiClicException.java @@ -1,47 +0,0 @@ -package exception; - -import java.io.Serializable; - -import com.google.gson.Gson; - -public class PtiClicException extends Exception { - - private static final long serialVersionUID = 1L; - private Error error; - - private static class Error implements Serializable { - - private static final long serialVersionUID = 1L; - private int num; - private String msg; - - public Error() {} - - public Error(int num, String msg) { - this.num = num; - this.msg = msg; - } - - public int getNum() { - return num; - } - - public String getMsg() { - return msg; - } - } - - public PtiClicException(int num, String msg) { - this.error = new Error(num, msg); - } - - public PtiClicException(String json) { - Gson gson = new Gson(); - error = gson.fromJson(json, Error.class); - } - - public String getMessage() { - return "Erreur numero: " + error.getNum() + "\n" + error.getMsg(); - } - -} diff --git a/code/PtiClic/src/org/pticlic/Score.java b/code/PtiClic/src/org/pticlic/Score.java @@ -1,11 +1,16 @@ package org.pticlic; +import org.pticlic.exception.PtiClicException; import org.pticlic.model.Constant; import org.pticlic.model.Match; import org.pticlic.model.Network; import org.pticlic.model.Network.Mode; + import android.app.Activity; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; @@ -20,7 +25,7 @@ import android.widget.Button; */ public class Score extends Activity implements OnClickListener{ - private Match gamePlayed; + private Match gamePlayed; @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,7 +47,23 @@ public class Score extends Activity implements OnClickListener{ Network network = new Network(serverURL, mode, id, passwd); // FIXME : Pour l'instant ne marche pas, attend de savoir comment est formater le score que l'on recois. - //DownloadedScore score = network.sendGame(gamePlayed); + try { + network.sendGame(gamePlayed); + } catch (PtiClicException e) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getString(R.string.app_name)) + .setIcon(android.R.drawable.ic_dialog_alert) + .setMessage(e.getMessage()) + .setCancelable(false) + .setNegativeButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + finish(); + } + }); + AlertDialog alert = builder.create(); + alert.show(); + } ((Button)findViewById(R.id.saw)).setOnClickListener(this); diff --git a/code/PtiClic/src/org/pticlic/exception/PtiClicException.java b/code/PtiClic/src/org/pticlic/exception/PtiClicException.java @@ -0,0 +1,51 @@ +package org.pticlic.exception; + +import java.io.Serializable; + +import com.google.gson.Gson; + +public class PtiClicException extends Exception { + + private static final long serialVersionUID = 1L; + private Error error; + + public static class Error implements Serializable { + + private static final long serialVersionUID = 1L; + private int num; + private String msg; + + public Error() {} + + public Error(int num, String msg) { + this.num = num; + this.msg = msg; + } + + public int getNum() { + return num; + } + + public String getMsg() { + return msg; + } + } + + public PtiClicException(Error error) { + this.error = error; + } + + public PtiClicException(int num, String msg) { + this.error = new Error(num, msg); + } + + public PtiClicException(String json) { + Gson gson = new Gson(); + error = gson.fromJson(json, Error.class); + } + + public String getMessage() { + return "Erreur numero: " + error.getNum() + "\n" + error.getMsg(); + } + +} diff --git a/code/PtiClic/src/org/pticlic/games/BaseGame.java b/code/PtiClic/src/org/pticlic/games/BaseGame.java @@ -2,6 +2,7 @@ package org.pticlic.games; import org.pticlic.R; import org.pticlic.Score; +import org.pticlic.exception.PtiClicException; import org.pticlic.model.Constant; import org.pticlic.model.DownloadedBaseGame; import org.pticlic.model.Match; @@ -9,7 +10,6 @@ import org.pticlic.model.Network; import org.pticlic.model.Network.Mode; import org.pticlic.model.Relation; -import exception.PtiClicException; import android.app.Activity; import android.app.AlertDialog; @@ -234,7 +234,6 @@ public class BaseGame extends Activity implements OnClickListener { leaveView(); start(); } else { - network.sendGame(match); Intent intent = new Intent(this, Score.class); intent.putExtra(Constant.SCORE_GAMEPLAYED, match); intent.putExtra(Constant.SCORE_MODE, Mode.SIMPLE_GAME); diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java @@ -8,13 +8,14 @@ import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URL; +import org.pticlic.exception.PtiClicException; + import android.content.Context; import android.net.ConnectivityManager; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; -import exception.PtiClicException; /** * @author Bertrand BRUN @@ -235,8 +236,14 @@ public class Network { reader.endObject(); return new DownloadedBaseGame(id, gid, pgid, cat1, cat2, cat3, cat4, center, cloud); } - - public TotalScore sendGame(Match game) { + + /** + * 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 Le score sous forme JSON. + */ + public TotalScore sendGame(Match game) throws PtiClicException { switch (mode) { case SIMPLE_GAME: return sendBaseGame(game); @@ -244,15 +251,14 @@ public class Network { return null; } } - - /** - * 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 Le score sous forme JSON. - */ - public TotalScore sendBaseGame(Match game) { + + + public TotalScore sendBaseGame(Match game) throws PtiClicException { TotalScore score = null; + URL url = null; + Gson gson = null; + BufferedReader reader = null; + String json = null; try { // TODO : ne restera le temps que les requete du serveur passe du GET au POST @@ -278,7 +284,7 @@ public class Network { urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat4(); } - URL url = new URL(urlS); + url = new URL(urlS); // URL url = new URL(this.serverURL); // URLConnection connection = url.openConnection(); @@ -288,16 +294,30 @@ public class Network { // connection.addRequestProperty("mode", mode.value()); // connection.addRequestProperty("pgid", String.valueOf(game.getGame().getId())); - Gson gson = new Gson(); -// JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); - JsonReader reader = new JsonReader(new InputStreamReader(url.openStream(), "UTF-8")); - - score = gson.fromJson(reader, TotalScore.class); + reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); + json = reader.readLine(); + + gson = new Gson(); + //JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + InputStream in = new ByteArrayInputStream(json.getBytes("UTF-8")); + JsonReader jsonReader = new JsonReader(new InputStreamReader(in)); + // Comme gson ne renvoie pas une erreur si l'objet qui recupere ne correspond pas a la classe qu'il attends. + // On creer tout d'abord une objet error et si celui-ci est vide on creer l'objet score, sinon on lance + // une exception. + PtiClicException.Error error = gson.fromJson(json, PtiClicException.Error.class); + if (error.getMsg() == null) { + score = gson.fromJson(jsonReader, TotalScore.class); + } else { + throw new PtiClicException(error); + } - } catch (IOException e) { - return score; + } catch (UnsupportedEncodingException e1) { + throw new PtiClicException(0, "Impossible de recuperer l'erreur, nous avons pris note de cette erreur.\n Merci"); + } catch (IOException e1) { + throw new PtiClicException(0, "Impossible de recuperer l'erreur, nous avons pris note de cette erreur.\n Merci"); } + return score; } }