j'ai essayé le code suivant:
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class maClasse extends MIDlet implements CommandListener {
private Display display;
///////// variables formulaire /////////
Form formulaire = null;
TextField login = null;
TextBox txtResults = null;
private Command cmdSubmit;
private Command cmdBack;
private Command cmdExit;
//////////// fin ///////////////////////
String url = "http://127.0.0.1/test.php?login=monlogin&pass=monpass";
public maClasse() {
display = Display.getDisplay(this);
///////////////////////
cmdSubmit = new Command("Valider", Command.SCREEN, 1);
cmdBack = new Command("Retour", Command.BACK, 0);
cmdExit = new Command("Quitter", Command.EXIT, 1);
///////////////////////
}
public void startApp() {
//////////////////////
formulaire = new Form("monsite : test");
login = new TextField("Entrez votre login :", null, 50, TextField.ANY);
formulaire.append(login);
formulaire.addCommand(cmdSubmit);
formulaire.addCommand(cmdExit);
formulaire.setCommandListener(this);
display.setCurrent(formulaire);
/////////////////////
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
void connectURL(String url) throws IOException {
HttpConnection connection = null;
InputStream is = null;
OutputStream os = null;
StringBuffer stringBuffer = new StringBuffer();
TextBox textBox = null;
try {
connection = (HttpConnection) Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-EN");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os = connection.openOutputStream();
is = connection.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
stringBuffer.append((char) ch);
}
textBox = new TextBox("Connexion...", stringBuffer.toString(), 1024, 0);
}
finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(connection != null) {
connection.close();
}
}
display.setCurrent(textBox);
}
public void commandAction(Command c, Displayable d) {
String str = c.getLabel();
if (str.equals("Quitter")) {
destroyApp(true);
return;
}
else if (str.equals("Retour")) {
display.setCurrent(formulaire);
return;
}
//////////// appel de connectURL ////////
try {
connectURL(url);
}
catch (IOException e) {
System.out.println("IOException " + e);
e.printStackTrace();
}
////////////////////////////////////////
}
}
le message suivant s'affiche :
To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
et je doit avoir un champ pour la saisie.