commit 13fa4de19a1bf74947b16887dffc49adae763ec6
parent 971a71bf991fa341d830bcf60ea22eb7a13cc1e7
Author: Bertrand BRUN <bertrand0brun@gmail.com>
Date: Sat, 12 Feb 2011 12:00:58 +0100
Gestion des erreurs dans la methode sendGame de la classe Network
Diffstat:
4 files changed, 64 insertions(+), 24 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/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/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;
}
}