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
|
// contient les éléments de base
import javax.microedition.midlet.*;
// contient les éléments permettant de gérer l'interface
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class HelloWorld extends MIDlet implements CommandListener
{
private Display _display;
private TextField _textField1;
private TextField _textField2;
private Command _commandOK1;
private Command _commandExit1;
private Form _form1;
private Alert alert = new Alert ("OK", "", null, AlertType.ERROR);
public HelloWorld()
{
// Creation d'un formulaire
_form1 = new Form("Test de Application");
// Creation boutons
_commandExit1 = new Command("ANNULER", Command.EXIT,1);
_commandOK1 = new Command("OK", Command.OK, 1);
// creation d'un champ de texte contenant notre Hello World
_textField1 = new TextField("Nom : ","",10,TextField.ANY);
_textField2 = new TextField("Mot de passe ","",10,TextField.ANY);
// ajout des composants au formulaire d'entrée
_form1.append("Veuillez entrer vos identifiants svp :");
_form1.append(_textField1);
_form1.append(_textField2);
_form1.addCommand(_commandExit1);
_form1.addCommand(_commandOK1);
_form1.setCommandListener(this);
}
// évènement exécuté au démarrage de l'application
public void startApp()
{
try{// Lien avec l'affichage
_display = Display.getDisplay(this);
// affichage du formulaire
_display.setCurrent(_form1);}
catch (Exception e)
{
e.printStackTrace();
}
}
// évènement exécuté lors de la mise en pause de l'application
public void pauseApp()
{
}
// évènement exécuté lorsque l'application se termine
public void destroyApp(boolean unconditional)
{
// appel manuel à la fonction de fermeture
destroyApp(false);
// on demande au manager de fermer l'application
notifyDestroyed();
}
public void commandAction(Command c, Displayable s)
{
// lors du clic sur le bouton Exit
if (c == _commandExit1)
{
// appel manuel à la fonction de fermeture
destroyApp(false);
// on demande au manager de fermer l'application
notifyDestroyed();
}
else if (c == _commandOK1)
{
//String t = sendPostHttpRequest("http://82.230.200.173/fichier.php");
//t = sendPostHttpRequest("http://82.230.200.173/demo6/sync/s.php");
alert.setString("OK");
_display.getDisplay(this).setCurrent(alert);
}
}
public String sendPostHttpRequest(String url)
{
HttpConnection c = null;
InputStream is = null;
DataOutputStream dos = null;
StringBuffer sb = new StringBuffer();
int rc;
String params = new String("");
try
{
c = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT" );
c.setRequestProperty("Content-Language", "en-US" );
c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Type", "**");
dos = c.openDataOutputStream();
byte [] byteRequest = params.getBytes ();
for (int i = 0; i < byteRequest.length; i ++)
{
dos.writeByte (byteRequest [i]);
}
}
catch (IOException e)
{
alert.setString("BUG");
_display.getDisplay(this).setCurrent(alert);
}
finally
{
try
{
if( c != null ) c.close();
if( is != null ) is.close();
if( dos != null ) dos.close();
}
catch ( IOException ioe )
{
ioe.printStackTrace();
}
}
return ("Connexion réussie");
}
} |
Partager