| Reply | « Previous Thread | Next Thread » |
|
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.) |
|
Hi gorbius,
Forum Nokia has a good example regarding HttpConnections and general thread usage. Take a look at this doc: MIDP 1.0: Introduction to Networked MIDlets v1.0 http://www.forum.nokia.com/info/sw.n..._v1_0.zip.html Check the HttpPoster.java as well as the other enclosed classes. HttpPoster.java implements the Runnable interface (a thread)... I hope it helps! BR |
|
yes, and down and dirty method for quick testing is like this...
Thread httpsThread = new Thread() { public void run() { 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(); }//end thread run };//end thread httpsThread.start(); Jason Glass http://IChiBanComputers.Com |
|
- implement thread and start thread [ inside command code]
- put 'what is to be done' code inside void run() this should solve your problem do put in 'implements CommandListener,Runnable' [ here I assume you're to use command button to perform a task ] |
| pixels_java |
| View Public Profile |
| Find all posts by pixels_java |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| How do i pass value from an xhtml page to a php page and likewise? | vash82 | Browsing and Mark-ups | 0 | 2005-02-17 07:15 |
| redirecting a page with PHP | edmacovaz | Browsing and Mark-ups | 9 | 2004-11-17 03:11 |
| J2ME MIDP 1.0 HTTPConnection POST and PHP page | alexandra_bustamante | Mobile Java Networking & Messaging & Security | 0 | 2004-07-22 20:18 |
| Problems viewing WML page with PHP code | Licho | Browsing and Mark-ups | 1 | 2004-07-16 10:21 |
| Page not found error - newbie | trinitypete | General Browsing | 0 | 2003-11-11 13:18 |