×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    HttpConnection Connect.Open() wont work

    Hello everybody,
    I have made a MIDlet to connect to a server of mine...
    Because of reusability I decided to make a Connection class obviously extending the Thread Class... This Connection class has several methods (send, receive etc)....
    Apart from my midlet and my Connection class I have a ConnectionGUI class which is actually an extension of the Form class (and implements commandlistener as well) and is nothing more than a GUI i want to use to show show of the connection details/data...

    The mechanics are :
    My MIDlet starts up and doesnt do much unless making an object of ConnectionGUI and placing that on the Display.
    Now I can enter something in ConnectionGUI object (thin username and password through two textfields).. Then I should be possible to push my connect button... In the commandAction method I actually want to call one of the beforementioned methods (send, receive) of the Threaded Connection class... problem is that it will ask me for permission but wont react on my input...
    If I place the same code in the run() method (instead of a own mehod) it will run though... AND if I place the call to the threaded method inside the MIDlet itself it will work.. Any idea why it will work if I call it from the MIDlet itself and it wont work if I place a class inbetween them ???
    So MIDlet --> Connection.send() will work but
    MIDlet --> ConnectionGUI.commandAction() --> Connection.send() wont

    As far as I can see I make the Connection object before the commandActio method i called and it even starts up its thread before that, so I actually only call the wanted method in commandAction... I read u dont want big stuff happening in there so I thought that Connection Thread/class would go on for itself...

    any help/tips or whatever is appreciated... I also am willingb to post some code if it helps out...

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    8

    Re: HttpConnection Connect.Open() wont work

    commandAction() would block other thread, maybe you can try start a new thread to call the connect.open(...) method to avoid this problem.

  3. #3
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: HttpConnection Connect.Open() wont work

    ok so lets say I start the thread long before commandAction gets called... Now I do want to call a method in that thread through CommandAcion... so user pushes button and commandAction has to call the method in the thread... That wont work, and Im trying to figure out why it wont work, whilst it will work if I call it from the MIDlet itself (and not my own class which supplies a GUI for Connection)...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    8

    Re: HttpConnection Connect.Open() wont work

    if the method been called in CommandAction() cannot return right away, the midlet might miss the method. (such method include Connect.open() )
    you can try new a thread in commandAction() to call such method, if it still not work, then I don't know the reason either, sorry.

    example:
    public void commandAction(...)
    {
    ...
    new Thread() {
    public void run() {
    // todo : method called here
    }
    }.start();
    ...
    }

  5. #5
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: HttpConnection Connect.Open() wont work

    well I dont want to write the code for the Thread itself within the commandAction method cause its quite a lot, not to mention re-usability

    so this is what I've done

    public void commandAction(Command c, Displayable d)
    {
    if(c == exitCommand)
    {

    }
    else if(c == okCommand)
    {
    myConnection.connect();
    }

    }

    myConnection is an instance of my Connection class which extends Thread...
    In the constructor of my class in which the CommandAction is defined I create an object of Connection and fire it up as well :

    myConnection = new Connection(mMidlet);
    myConnection.start();

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Re: HttpConnection Connect.Open() wont work

    new Thread(new Runnable() {
    public void run(){
    // here u can call ur connection class function
    }
    }).start();

    u can use like this it will work fone

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    8

    Re: HttpConnection Connect.Open() wont work

    Quote Originally Posted by Tiger79
    well I dont want to write the code for the Thread itself within the commandAction method cause its quite a lot, not to mention re-usability
    I see... However, my suggestion is that you can take it as a try, if it works, the WHY can be found out!

    public void commandAction(Command c, Displayable d)
    {
    if(c == exitCommand)
    {

    }
    else if(c == okCommand)
    {
    new Thread() {
    public void run() {
    myConnection.connect();
    }
    }.start();
    }

    }

  8. #8
    Regular Contributor
    Join Date
    May 2003
    Posts
    151

    Re: HttpConnection Connect.Open() wont work

    Quote Originally Posted by laxcrab
    public void commandAction(Command c, Displayable d)
    {
    if(c == exitCommand)
    {

    }
    else if(c == okCommand)
    {
    new Thread() {
    public void run() {
    myConnection.connect();
    }
    }.start();
    }

    }

    That's a really bad idea. Starting a thread takes quite a while compared to other operations. The commandAction function needs to return really really quickly, so you must have the thread already started and then just listening for a flag that is set in CommandAction.

    I decided to make a Connection class obviously extending the Thread Class
    This is wrong - you meant to implement the Runable interface and then run your class through the Thread.start() method, which is much more likely to give you the results you're after.

    As an aside extending the Thread Class is evil - unless you really really know what you're doing.

  9. #9
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: HttpConnection Connect.Open() wont work

    Well,
    could you help me out then, seems like you know what your talking about

    anyways lets say I have a Connection class with two methods : sendData end receiveData... This implements the Runnable Interface

    So my midlet fires up, i get in the startApp() method and I do my initializing and stuff. My midlet has a Form with a Command to send data, and a CommandListener Interface.
    I guess I could start off my thread right away in the startApp() method ? as in :

    Connection conn = new Connection();
    Thread t = new Thread(conn);
    t.start();

    oki so the I press my send Data button, and I get thrown into the commandAction method

    if(c == sendDataCmd)
    {
    }

    oki, so now I'd love to know how I have to fill this up ? is it with conn.sendData() ? remember my run() method in Connection class is simply empty...
    cause this has been my problem, if I place a call to the sendData method in run it will run just fine, instead if i dont place it in run method and call it from the commandAction method as in :

    f(c == sendDataCmd)
    {
    conn.sendData();
    }

    it will just freeze up...

    Any help, hints or tips are welcome
    Last edited by Tiger79; 2007-06-13 at 16:49.

  10. #10
    Regular Contributor
    Join Date
    May 2003
    Posts
    151

    Re: HttpConnection Connect.Open() wont work

    Quote Originally Posted by Tiger79
    I guess I could start off my thread right away in the startApp() method ? as in :
    You probably never want to be calling a httpConnection thread directly as http connections can lock up and the thread can die. You need to have a main thread that runs and inserts requests for and probably isn't go.


    Anyway here is something quick and dirty*

    Code:
    class MyMidlet extends MIDlet implements Runnable, CommandListener{
    
        boolean isStarted = false;
        Thread thread;
        boolean sendDataWasPressed = false;
    
        protected void startApp(){
            if (isStarted == false) {
                isStarted = true;
    
                thread = new Thread(this);
                thread.start();            
            }
            else {
                //startFromPaused();
            }
        }
    
        public void commandAction(Command c, Displayable d) {
            if(c == sendDataCmd){
                sendDataWasPressed = true;
            }
        }
    
        public void run() {
    
            MyHttpConnection myHttpConnection = new MyHttpConnection();
            
            //Create your form here and attach the commands to it
    
            while(quitApplication == false){
    
                if(myHttpConnection.hasTimedOut() == true){
                    myHttpConnection.setDead();
                    myHttpConnection = new MyHttpConnection();
                }
    
                if( sendDataWasPressed == true){
                    sendDataWasPressed = false
                    myHttpConnection.sendSomeData();
                }
                
                //feel free to do something with the data received from the connection here.
    
                 Thread.sleep(50);//sleep for a bit to give other threads a chance to run.
    
            }
        }
    }
    
    
    class MyHttpConnection implements Runnable{
    
        Vector dataToSend = new Vector();
        Thread currentThread;
    
        boolean dead = false;
    
        public MyHttpConnection(){
            currentThread = new Thread(this);
            currentThread.start();
        }
        
        public sendSomeData(){
            dataToSend.addElement(...someData...);
        }
    
        public setDead(){
            dead = true;
        }
    
        public void run(){
            
            Connection conn = new Connection();
            
            while(dead == false){
                if(dataToSend.size() > 0){
                    // get the first peice of data to send
                    // and remove it from the queue of things to send.
                }
            }
            
            //connection has died - try to do some cleanup and hope for the best. 
        }
    }

    *just like I like my coffee

  11. #11
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: HttpConnection Connect.Open() wont work

    hhhmmm... ok, this looks a little like an example I found in the meantime...
    I noticed both sources have the Midlet expressively stated as a Runnable, but I always thought a MIDlet would standard start on a thread of itself ?
    That would have been a big misundrestanding on my behalf....
    Im gonna have a good look at your code now, and see if i can clear up my thoughts about al this networking/threading thing

    I hope I can get some help if I stumble up to problems or questions, can pay in coffee

    and BTW a little question about the code :

    Connection conn = new Connection();

    Is this another class ? Or is that myhttpConnection class some kind of interface ?
    Last edited by Tiger79; 2007-06-14 at 08:58.

  12. #12
    Regular Contributor
    Join Date
    May 2003
    Posts
    151

    Re: HttpConnection Connect.Open() wont work

    Quote Originally Posted by Tiger79
    is that myhttpConnection class
    It's a class - I suggest you take it from the MIDP 1.0: Fruit Machine Example
    http://www.forum.nokia.com/info/sw.n..._v1_0.zip.html

Similar Threads

  1. 6270 MIDP SocketConnection test work fine on emulator but fail on real phone
    By mtiger2k in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2008-12-13, 13:06
  2. multiple flush() not work for j2me httpconnection?
    By tiansun in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2006-08-15, 18:59
  3. Nokia 6600 Network (Http and Socket) connections do not work!
    By jimmichr in forum Mobile Java Networking & Messaging & Security
    Replies: 11
    Last Post: 2006-07-14, 07:48
  4. HttpConnection on Nokia 3586i
    By chrisdon2121 in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2006-04-01, 23:25
  5. Nokia 6600 Network (Http and Socket) connections do not work!
    By jimmichr in forum Mobile Java General
    Replies: 2
    Last Post: 2003-11-23, 20:42

Posting Permissions

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