BomberStudentReponse.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package rsx;
  2. import org.json.JSONObject;
  3. import org.json.JSONException;
  4. /**
  5. * Analyse reponse d'un serveur BomberStudent
  6. *
  7. * @author Arthur Brandao
  8. */
  9. public class BomberStudentReponse {
  10. /**
  11. * Reponse valide ou non
  12. */
  13. protected boolean error = true;
  14. /**
  15. * Code du statut encoyé par le serveur
  16. */
  17. protected int statut;
  18. /**
  19. * Message du statut envoyé par le serveur
  20. */
  21. protected String message;
  22. /**
  23. * Information en JSON envoyé par le serveur
  24. */
  25. protected JSONObject reponse = null;
  26. /* --- Constructeurs --- */
  27. /**
  28. * Constructeur par defaut en cas de reponse invalide
  29. */
  30. public BomberStudentReponse() {
  31. }
  32. /**
  33. * Parse la reponse du serveur
  34. * @param reponse La reponse en JSON à parser
  35. */
  36. public BomberStudentReponse(String reponse) {
  37. this.error = false;
  38. //Parse reponse
  39. try {
  40. JSONObject json = new JSONObject(reponse);
  41. this.statut = json.getInt("statut");
  42. this.message = json.getString("message");
  43. if(json.has("param")){
  44. this.reponse = json.getJSONObject("param");
  45. }
  46. this.error = false;
  47. } catch(JSONException ex){
  48. System.err.println("La reponse n'est pas en JSON : " + ex.getMessage());
  49. }
  50. }
  51. /* --- Getter/Setter --- */
  52. public boolean isError() {
  53. return error;
  54. }
  55. public void setError(boolean error) {
  56. this.error = error;
  57. }
  58. public int getStatut() {
  59. return statut;
  60. }
  61. public void setStatut(int statut) {
  62. this.statut = statut;
  63. }
  64. public String getMessage() {
  65. return message;
  66. }
  67. public void setMessage(String message) {
  68. this.message = message;
  69. }
  70. public JSONObject getReponse() {
  71. return reponse;
  72. }
  73. public void setReponse(JSONObject reponse) {
  74. this.reponse = reponse;
  75. }
  76. }