×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    newbie in trouble with invoking php page

    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.)

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: newbie in trouble with invoking php page

    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

  3. #3
    Super Contributor
    Join Date
    Mar 2006
    Location
    Phoenix, AZ. USA
    Posts
    556

    Re: newbie in trouble with invoking php page

    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

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Re: newbie in trouble with invoking php page

    - 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 ]

Similar Threads

  1. Replies: 0
    Last Post: 2005-02-17, 06:15
  2. redirecting a page with PHP
    By edmacovaz in forum Browsing and Mark-ups
    Replies: 9
    Last Post: 2004-11-17, 02:11
  3. J2ME MIDP 1.0 HTTPConnection POST and PHP page
    By alexandra_bustamante in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2004-07-22, 19:18
  4. Problems viewing WML page with PHP code
    By Licho in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2004-07-16, 09:21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •