Advertisement
Guest User

Untitled

a guest
May 14th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package govoyage;
  7.  
  8. import java.io.DataInputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import javax.microedition.io.Connector;
  13. import javax.microedition.io.HttpConnection;
  14. import javax.microedition.lcdui.Command;
  15. import javax.microedition.lcdui.CommandListener;
  16. import javax.microedition.lcdui.Display;
  17. import javax.microedition.lcdui.Displayable;
  18. import javax.microedition.lcdui.Form;
  19. import javax.microedition.lcdui.StringItem;
  20. import javax.microedition.lcdui.TextField;
  21. import javax.microedition.midlet.*;
  22.  
  23. /**
  24. * @author khlifi
  25. */
  26. public class Midlet extends MIDlet implements CommandListener {
  27.  
  28. Display disp = Display.getDisplay(this);
  29. Form AccueilForm = new Form("Welcome to GoVoyage");
  30. Command Connecte = new Command("Connecte", Command.OK, 0);
  31. Command Inscrire = new Command("Inscrire", Command.OK, 0);
  32. //Authentification stuff
  33. Form AuthentificationForm = new Form("Authentification");
  34. TextField name = new TextField("name :", null, 50, TextField.ANY);
  35. TextField password = new TextField("password :", null, 50, TextField.PASSWORD);
  36. Command cnx = new Command("connexion", Command.OK, 0);
  37. //Inscription stuff
  38. Form InscriptionForm = new Form("Authentification");
  39. TextField InscriptionName = new TextField("Nom Utlisateur :", null, 50, TextField.ANY);
  40. TextField InscriptionMail = new TextField("Adresse email :", null, 50, TextField.ANY);
  41. TextField InscriptionPassword = new TextField("Mot de passe :", null, 50, TextField.PASSWORD);
  42. TextField InscriptionPassword2 = new TextField("Rétapez Mot de passe :", null, 50, TextField.PASSWORD);
  43. Command inscrire = new Command("S'inscrire", Command.OK, 0);
  44. //Dashboard stuff
  45. /* Form InscriptionForm = new Form("Authentification");
  46. TextField InscriptionName = new TextField("Nom Utlisateur :", null, 50, TextField.ANY);
  47. TextField InscriptionMail = new TextField("Adresse email :", null, 50, TextField.ANY);
  48. TextField InscriptionPassword = new TextField("Mot de passe :", null, 50, TextField.PASSWORD);
  49. TextField InscriptionPassword2 = new TextField("Rétapez Mot de passe :", null, 50, TextField.PASSWORD);
  50. Command inscrire = new Command("S'inscrire", Command.OK, 0);*/
  51.  
  52. HttpConnection hc = null;
  53. InputStream in = null;
  54. OutputStream out = null;
  55.  
  56. public void startApp() {
  57. AccueilForm.addCommand(Connecte);
  58. AccueilForm.setCommandListener((CommandListener) this);
  59. AccueilForm.addCommand(Inscrire);
  60.  
  61. disp.setCurrent(AccueilForm);
  62. }
  63.  
  64. public void pauseApp() {
  65. }
  66.  
  67. public void destroyApp(boolean unconditional) {
  68. }
  69.  
  70. public void commandAction(Command c, Displayable d) {
  71. if (c == Connecte) {
  72. AuthentificationForm.append(name);
  73. AuthentificationForm.append(password);
  74. AuthentificationForm.addCommand(cnx);
  75. AuthentificationForm.setCommandListener((CommandListener) this);
  76. disp.setCurrent(AuthentificationForm);
  77. }
  78. if (c == Inscrire) {
  79. InscriptionForm.append(InscriptionName);
  80. InscriptionForm.append(InscriptionMail);
  81. InscriptionForm.append(InscriptionPassword);
  82. InscriptionForm.append(InscriptionPassword2);
  83. InscriptionForm.addCommand(inscrire);
  84. InscriptionForm.setCommandListener((CommandListener) this);
  85. disp.setCurrent(InscriptionForm);
  86. }
  87. if (c == cnx) {
  88. Authentification();
  89. }
  90. if (c == inscrire) {
  91. Inscription();
  92. }
  93. }
  94.  
  95. public void Authentification() {
  96.  
  97. try {
  98. String message = "username=" + name.getString().trim() + "&password=" + password.getString().trim();
  99. String url = "http://localhost/GoVoyage/web/app_dev.php/loginJME";
  100. hc = (HttpConnection) Connector.open(url);
  101. hc.setRequestMethod(HttpConnection.POST);
  102. hc.setRequestProperty("Content-Type",
  103. "application/x-www-form-urlencoded");
  104. hc.setRequestProperty("Content-Length",
  105. Integer.toString(message.length()));
  106. out = hc.openOutputStream();
  107. out.write(message.getBytes());
  108. in = hc.openInputStream();
  109. int length = (int) hc.getLength();
  110. byte[] data = new byte[length];
  111. in.read(data);
  112. String response = new String(data);
  113. StringItem stringItem = new StringItem(null, response);
  114. AuthentificationForm.append(stringItem);
  115. AuthentificationForm.setTitle("Done.");
  116. } catch (IOException ioe) {
  117. StringItem stringItem = new StringItem(null, ioe.toString());
  118. AuthentificationForm.append(stringItem);
  119. AuthentificationForm.setTitle("Done.");
  120. } finally {
  121.  
  122. try {
  123. if (out != null) {
  124. out.close();
  125. }
  126. if (in != null) {
  127. in.close();
  128. }
  129. if (hc != null) {
  130. hc.close();
  131. }
  132. } catch (IOException ex) {
  133. ex.printStackTrace();
  134. }
  135.  
  136. }
  137. }
  138.  
  139. public void Inscription() {
  140.  
  141. try {
  142. String message = "fos_user_registration_form[username]=" + InscriptionName.getString().trim() + "&fos_user_registration_form[plainPassword][first]=" + InscriptionPassword.getString().trim() + "&fos_user_registration_form[plainPassword][second]=" + InscriptionPassword.getString().trim() + "&fos_user_registration_form[email]=" + InscriptionMail.getString().trim();
  143. String url = "http://localhost/GoVoyage/web/app_dev.php/register/";
  144. hc = (HttpConnection) Connector.open(url);
  145. hc.setRequestMethod(HttpConnection.POST);
  146. hc.setRequestProperty("Content-Type",
  147. "application/x-www-form-urlencoded");
  148. hc.setRequestProperty("Content-Length",
  149. Integer.toString(message.length()));
  150. out = hc.openOutputStream();
  151. out.write(message.getBytes());
  152. in = hc.openInputStream();
  153. int length = (int) hc.getLength();
  154. byte[] data = new byte[length];
  155. in.read(data);
  156. String response = new String(data);
  157. StringItem stringItem = new StringItem(null, response);
  158. AuthentificationForm.append(stringItem);
  159. AuthentificationForm.setTitle("Done.");
  160. } catch (IOException ioe) {
  161. StringItem stringItem = new StringItem(null, ioe.toString());
  162. AuthentificationForm.append(stringItem);
  163. AuthentificationForm.setTitle("Done.");
  164. } finally {
  165.  
  166. try {
  167. if (out != null) {
  168. out.close();
  169. }
  170. if (in != null) {
  171. in.close();
  172. }
  173. if (hc != null) {
  174. hc.close();
  175. }
  176. } catch (IOException ex) {
  177. ex.printStackTrace();
  178. }
  179.  
  180. }
  181. }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement