tags:

views:

317

answers:

3

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("http://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.

+1  A: 

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.

Sean Owen
A: 

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.

Fostah
A: 

Hi im not a member so i wrote my problem in here can someone reply me my cod make conection in sonyerricson mobile and work but no work in nokia mobile i think its because cant run second thread(the first should stoped). pleas mail you answer to me whit thanks Mail : [email protected]

.

 public int connectToServer (final String serverIP, final String serverPort,Display disp) throws InterruptedException
{
    maydisplay=disp;
    this.serverIP = serverIP;
    this.serverPort = serverPort;
   if (myConnecctionThread == null) {
        myConnecctionThread = new Thread(mc);
    }      
    myConnecctionThread.start();  

        return 1;
}

MakeConnetion mc=new MakeConnetion();
 public class  MakeConnetion implements Runnable 
{
     public void run()
        {
        try {
            AppInfo.LogToFile("Run nn nn   ");
                connection = (StreamConnection) Connector.open("socket://" + serverIP + ":" + serverPort);
                setConnected(true);
                out = connection.openDataOutputStream();
                in = connection.openInputStream();
        try
        {
            while (true)
            {
                byte buffer[] = new byte[4096];
                int size = in.available
                in.read(buffer);
                size = in.available();
                String data = "";
                for (int i = 0; i < 4096 && buffer[i] != 0; i++)
                    data += (char) buffer[i];
                if (tcpEvents != null)
                {
                    if (buffer[0] != 0)
                    {
                       tcpEvents.OnRecivedData(data);
                    }
                    else
                    {
                        connection.close();
                        in.close();

...

Phcraft