1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
public class DomoBox extends MIDlet implements CommandListener, Runnable {
/**URL HTTP du serveur domobox*/
private String url ="http://daviddurand.info/D228/domo";
private Display display;
private Form form;
// private TextField login, password;
StringBuffer buffer = new StringBuffer();
/**Commande pour quitter l'application*/
private Command cmdQuitter = new Command("Quitter", Command.CANCEL, 2);
/**Commande pour se connecter*/
private Command cmdConnect = new Command("Se Connecter", Command.OK, 2);
/**Commande pour afficher l'état des périphériques*/
private Command cmdEtat = new Command("Etat Périphériques", Command.ITEM, 1);
/**Commande pour modifier/mettre à jour un appareil*/
private Command cmdModifier = new Command("Modifier", Command.ITEM, 1);
/**Commande pour se déconnecter de la domobox*/
private Command cmdDeconnect = new Command("Se déconnecter", Command.ITEM, 1);
/**Commande pour réessayer son login et son mot de passe après une erreur d'authentification*/
private Command cmdRetry = new Command("Recommencer", Command.BACK, 1);
/**Champ Nom d'utilisateur*/
TextField login = new TextField("Nom d'utilisateur :", "", 20, TextField.ANY);
/**Champ Mot de passe*/
TextField password = new TextField("Mot de passe :", "", 20, TextField.PASSWORD);
private StringItem msgServeur;
boolean firstime = true;
private Command currentCommand;
private Thread commandThread;
private String domobox;
public void startApp() throws MIDletStateChangeException {
mainscreen ();
}
/**Accueil*/
private void mainscreen() {
form = new Form ("Connexion au DOMOBOX");
form.append(login);
form.append(password);
display = Display.getDisplay(this);
form.addCommand(cmdConnect);
form.addCommand(cmdQuitter);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
/****
* THREAD
*/
public void commandAction(Command c, Displayable d) {
synchronized (this) {
if (commandThread != null) {
// process only one command at a time
return;
}
currentCommand = c;
commandThread = new Thread(this);
commandThread.start();
}
}
public void run() {
if (currentCommand == cmdQuitter) {
destroyApp(false);
notifyDestroyed();
} else if (currentCommand == cmdConnect) {
try {
seConnecter(login.getString(),password.getString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (currentCommand == cmdEtat) {
System.out.println("Clique sur Etat !");
}
synchronized (this) {
// signal that another command can be processed
commandThread = null;
}
}
private void seConnecter(String u, String p) throws IOException {
form = new Form ("Connection...");
msgServeur = new StringItem("", "");
display = Display.getDisplay(this);
form.append(msgServeur);
form.addCommand(cmdEtat);
form.addCommand(cmdModifier);
form.addCommand(cmdDeconnect);
//formConnected.addCommand(cmdQuitter);
display.setCurrent(form);
/**Pour se connecter au serveur HTTP*/
HttpConnection c = null;
/**Réponse du serveur HTTP sous forme de flux*/
InputStream is = null;
try {
c = (HttpConnection)Connector.open(url+"/connect/"+u+"/"+p);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("cookie", domobox);
DataInputStream dis = new DataInputStream (c.openInputStream());
/**Récupération byte par byte de la réponse du serveur*/
int ch;
while ((ch = dis.read()) != -1) {
buffer.append((char)ch);
}
/***
*
* Un simple println sur l'ecan j'arrive à récupérer en String la réponse du serveur.
* */
//System.out.println("message du serveur : " + buffer.toString());
} catch( Exception e )
{
e.printStackTrace();
buffer.append( "ERROR CONNECTION HTTP" );
}
/**Free i/o Stream et Connection HTTP*/
finally {
if (is != null) {
is.close();
}
if (c != null) {
c.close();
}
}
msgServeur.setText(buffer.toString());
}
} |
Partager