Abhi
Abhi

Reputation: 2378

j2me network connection

I have read in many places that network connection in a j2me app should be done in a separate thread. Is this a necessity or a good to have?

I am asking this because I could not find anywhere written that this must be done in a separate thread. Also, when I wrote a simple app to fetch an image over a network and display it on screen (without using a thread) it did not work. When I changed the same to use a separate thread it worked. I am not sure whether it worked just because I changed it to a separate thread, as I had done many other changes to the code also.

Can someone please confirm?

Edit: If running in a separate thread is not a necessity, can someone please tell me why the below simple piece of code does not work?

It comes to a stage where the emulator asks "Is it ok to connect to net". Irrespective of whether I press an "yes" or a "no" the screen does not change.


public class Moo extends MIDlet {

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
    }

    protected void startApp() throws MIDletStateChangeException {
        Display display = Display.getDisplay(this);
        MyCanvas myCanvas = new MyCanvas();
        display.setCurrent(myCanvas);
        myCanvas.repaint();

    }

    class MyCanvas extends Canvas {

        protected void paint(Graphics graphics) {
            try {
                Image bgImage = Image.createImage(getWidth(), getHeight());

                HttpConnection httpConnection = (HttpConnection) Connector
                        .open("https://stackoverflow.com/content/img/so/logo.png");
                Image image = Image.createImage(httpConnection
                        .openInputStream());
                bgImage.getGraphics().drawImage(image, 0, 0, 0);
                httpConnection.close();

                graphics.drawImage(bgImage, 0, 0, 0);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}


Edit: I got my answer for the code here.

Edit: I spawned off a separate question of this here.

Upvotes: 1

Views: 1205

Answers (3)

jgatcher
jgatcher

Reputation: 175

It is not mandatory that you do network connections in a new thread,however practically you'll find that it almost always a good idea to do so since network activities could block and leave your app in an unresponsive state.

This is an old article but it speaks about some of the issues involved in networking and user experience.

Upvotes: 0

Fostah
Fostah

Reputation: 11816

I agree with Sean, but it is not required to have your network connection in a separate thread, just best practice. I think that it's probably coincidental that the connection worked properly after moving it to a separate thread. Either way though, if you want to provide any visual feedback to the user while the connection is happening (which you probably do considering the disparity of lag that users can experience on a mobile network), you should have the networked processing in a separate thread.

Upvotes: 0

Sean Owen
Sean Owen

Reputation: 66891

The problem is that you are trying to do work within the thread that is responsible for running the UI. If you do not use a separate thread, then that UI thread is waiting while you do your work and can't process any of your other UI updates! so yes you really should not do any significant work in event handlers since you need to return control quickly there.

Upvotes: 2

Related Questions