Hello,
I really hope someone can help me. I need to create an application which can create statements and people must be able to reply to these statements. So far so good. I have a content managment system in php that writes a question to a file. My Midlet reads this file en there is a form where people can answer (true/false) and a opnion. I put this answers in a string and show them in an alert.
from this part I want to send the answers to "http://localhost/java/answer.php?string="
I found several scripts online which should be able to do it but every time I try it I get: Warning: To avoid potential deadlock, operations that may block, such as networking, should be performed in a different thread than the
commandAction() handler.
So now you probaly think "use a thread!". But the thing is... al the examples I found on the web didn't use a thread. I'm willing to create a new thread but don't now anything about classes, threads, inheritage etc... in java that is
My working code is:
package stelling;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
//import javax.microedition.midlet.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
//import javax.microedition.lcdui.*;
public class stelling extends MIDlet implements CommandListener
{
private Display display;
//forms
private Form fmStelling;
private Form fmMain;
//commands
private Command cmSubmit;
private Command cmExit;
private Command cmBack;
private Command cmDownload;
private Command cmAvatar;
private Command cmStelling;
//vectoren
public Vector gegevens;
//strings
private String data = null;
private String url = "http://localhost/java/stelling.txt";
private String choices[] = {"Voor", "Tegen"};
//items
private TextField nameField;
private ChoiceGroup choiceField;
private TextField meningField;
//alerts
private Alert mModalAlert;
public stelling()
{
display = Display.getDisplay(this);
cmSubmit = new Command("Submit", Command.SCREEN, 1);
cmExit = new Command("Exit", Command.EXIT, 1);
cmDownload = new Command("Download", Command.SCREEN, 4);
cmAvatar = new Command("Avatar", Command.SCREEN, 2);
cmStelling = new Command("Stelling", Command.SCREEN, 3);
nameField = new TextField("Naam:", "", 30, TextField.ANY);
choiceField = new ChoiceGroup("Keuze", Choice.EXCLUSIVE,choices,null);
meningField = new TextField("Mening:", "", 50, TextField.ANY);
// formulieren
fmMain = new Form("MTV 4 MOBILES");
fmMain.setCommandListener(this);
fmStelling = new Form("Stelling");
// stelling vast ophalen
try {
downloadStelling(url);
} catch(IOException e) {
// handle the exception
}
//form main opmaken
fmMain.addCommand(cmExit);
fmMain.addCommand(cmDownload);
fmMain.addCommand(cmAvatar);
fmMain.addCommand(cmStelling);
// form stelling opmaken
fmStelling.addCommand(cmExit);
fmStelling.addCommand(cmSubmit);
fmStelling.append(nameField);
fmStelling.append(choiceField);
fmStelling.append(meningField);
fmStelling.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == cmStelling) {
System.out.println("we gaan wat doen... naar fmStelling ");
display.setCurrent(fmStelling);
}
if (command == cmSubmit){
StringItem data = new StringItem(null,"" + nameField.getString() + ";" + choiceField.getString(choiceField.getSelectedIndex()) + ";" + meningField.getString());
mModalAlert = new Alert("Resultaat","" + data.getText(),null,AlertType.INFO);
mModalAlert.setTimeout(Alert.FOREVER);
display.setCurrent(mModalAlert);
fmStelling.removeCommand(cmSubmit);
}
else if (command == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
private void downloadStelling(String url) throws IOException {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
TextBox t = null;
try {
long len = 0 ;
int ch = 0;
c = (HttpConnection)Connector.open(url);
is = c.openInputStream();
len =c.getLength() ;
if ( len != -1) {
// Read exactly Content-Length bytes
for (int i =0 ; i < len ; i++ )
if ((ch = is.read()) != -1)
b.append((char) ch);
} else {
// Read till the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available() ;
b.append((char)ch);
}
}
// stelling weergeven
StringItem stelling = new StringItem(null,"Stelling : " + b.toString() + "\n\n");
fmStelling.append(stelling);
} finally {
is.close();
c.close();
}
display.setCurrent(t);
}
}
I hope there is someone who can help me... i only need to send those values to a php page... Thanks in advance!!! (Sorry that some names are dutch... but i think the rest is clear.)


Reply With Quote


