Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    65
    Thanks
    3
    Thanked 0 Times in 0 Posts

    J2ME different thread for http connection

    I'm literally tearing my hair out trying to figure out how to do this! My code says that I need to put my http connection code in a different thread to the commandAction() handler, but i'm not sure how to do it.

    Here is my code:
    Code:
    //http://www.java-samples.com/showtutorial.php?tutorialid=731
    //http://www.java-tips.org/java-me-tips/midp/demonstration-of-choicegroup-gui-component.html -- pop up box thing
    
    import java.io.*;
    import javax.microedition.io.*;
    import java.io.IOException;
    import javax.microedition.lcdui.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    import javax.microedition.media.control.VideoControl;
    import javax.microedition.rms.*;
    
    public class JudgeMidlet extends MIDlet implements CommandListener {
        // display manager
        Display display = null;
        Form form = null;
        List menu = null; // main menu with items
        TextBox input = null;
        Canvas canvas = null;
        private ImageItem imageItem;
        private TextField userTags;
        private Player player;
        private VideoControl videoControl;
        private Video video;
        private ChoiceGroup Age = new ChoiceGroup("What is your age?", Choice.POPUP, new String[]{ "Teenager", "20-25", "26-30", "31-35", "36-40", "41-45", "46-50", "50+" }, null);
        private ChoiceGroup Music = new ChoiceGroup("What is your favuorite music?", Choice.POPUP, new String[]{ "R&B", "Rap", "Rock", "Pop", "Dubstep", "Heavy Metal", "Classical" }, null);
        private ChoiceGroup Tattoos = new ChoiceGroup("Do you have any tattoos?", Choice.POPUP, new String[]{ "Yes", "No" }, null);
        private ChoiceGroup Money = new ChoiceGroup("How much money do you earn?", Choice.POPUP, new String[]{ "Unemployed", "Around 10,000", "Around 20,000", "Around 30,000", "Around 40,000", "50,000+" }, null);
    
        // command
        static final Command backCommand = new Command("Back", Command.BACK, 0);
        static final Command mainMenuCommand = new Command("Main", Command.SCREEN, 1);
        static final Command exitCommand = new Command("Exit", Command.STOP, 2);
        static final Command capture = new Command("Capture", Command.SCREEN, 3);
        static final Command camera = new Command("Camera", Command.SCREEN, 3);
        static final Command upload = new Command("Upload", Command.SCREEN, 3);
        static final Command getImage = new Command("Get Image", Command.SCREEN, 3);
        String currentMenu = null;
        String currentForm = null;
    
        //String url = "http://student.dc.lincoln.ac.uk/~u08108703/judge_database.php";
        //String message = "";
    
        // constructor.
        public JudgeMidlet() {
        }
    
        /**
         * Start the MIDlet by creating a list of items and associating the
         * exit command with it.
         */
        public void startApp() throws MIDletStateChangeException {
          display = Display.getDisplay(this);
          menu = new List("Options", Choice.IMPLICIT);
          menu.append("Judge People", null);
          menu.append("Upload Your Own Photo", null);
          menu.addCommand(exitCommand);
          menu.setCommandListener(this);
    
          mainMenu();
        }
    
        public void pauseApp() {
          display = null;
          menu = null;
          input = null;
          form = null;
          canvas = null;
        }
    
        public void destroyApp(boolean unconditional) {
          notifyDestroyed();
        }
    
        // main menu
        void mainMenu() {
          display.setCurrent(menu);
          currentMenu = "Main";
        }
    
        /**
         * a generic method that will be called when selected any of
         * the items on the list.
         */
        public void judge() {
           form = new Form("Judge People");
           getNewImage();
           form.addCommand(backCommand);
           form.addCommand(getImage);
           form.setCommandListener(this);
           display.setCurrent(form);
        }
    
        public void camera() {
           form = new Form("Take a Photo");
           form.addCommand(backCommand);
           form.addCommand(camera);
           form.setCommandListener(this);
           display.setCurrent(form);
        }
    
           public void tags() {
           form.append(Age);
           form.append(Music);
           form.append(Tattoos);
           form.append(Money);
           form.addCommand(backCommand);
           form.addCommand(upload);
           form.setCommandListener(this);
           display.setCurrent(form);
        }
    
        public void upload(){
    //
        }
    
        /**
         * Test item1.
         */
        public void testItem1() {
          judge();
          currentMenu = "Judge People";
        }
    
        /**
         * Test item2.
         */
        public void testItem2() {
           camera();
           currentMenu = "Upload Your Own Photo";
        }
    
       /**
        * Handle events.
        */
       public void commandAction(Command c, Displayable d) {
          String label = c.getLabel();
          if (label.equals("Exit")) {
             destroyApp(true);
          } else if (label.equals("Get Image")) {
               judge();
               getNewImage();
          } else if (label.equals("Camera")) {
              showCamera();
          } else if (label.equals("Capture")) {
                video = new Video(this);
                video.start();
          } else if (label.equals("Upload")) {
                upload();
          }
          else if (label.equals("Back")) {
              if(currentMenu.equals("Judge People") || currentMenu.equals("Upload Your Own Photo"))  {
                // go back to menu
                form.deleteAll();
                mainMenu();
              }
          } else {
             List down = (List)display.getCurrent();
             switch(down.getSelectedIndex()) {
               case 0: testItem1();break;
               case 1: testItem2();break;
             }
    
          }
      }
    
       public void getNewImage() {
        HttpConnection hc = null;
        DataInputStream in = null;
    
        try {
          form.setTitle("Connecting ....");
          String url = "http://student.dc.lincoln.ac.uk/~uslawson/cctv/getimage.php";
    
          hc = (HttpConnection)Connector.open(url);
          int length = (int)hc.getLength();
          byte[] data = null;
          if (length != -1) {
            data = new byte[length];
            in = new DataInputStream(hc.openInputStream());
            in.readFully(data);
          }
          else {
            // If content length is not given, read in chunks.
            int chunkSize = 512;
            int index = 0;
            int readLength = 0;
            in = new DataInputStream(hc.openInputStream());
            data = new byte[chunkSize];
            do {
              if (data.length < index + chunkSize) {
                byte[] newData = new byte[index + chunkSize];
                System.arraycopy(data, 0, newData, 0, data.length);
                data = newData;
              }
              readLength = in.read(data, index, chunkSize);
              index += readLength;
            } while (readLength == chunkSize);
            length = index;
          }
          Image image = Image.createImage(data, 0, length);
          imageItem = new ImageItem(null, image, 0, null);
          form.append(imageItem);
          form.setTitle("What can you see in this image?");
          userTags = new TextField("Tag this image:", "", 256, TextField.ANY);
          form.append(userTags);
        }
        catch (IOException ioe) {
          StringItem stringItem = new StringItem(null, ioe.toString());
          form.append(stringItem);
          form.setTitle("ERROR!.");
        }
        finally {
          try {
            if (in != null) in.close();
            if (hc != null) hc.close();
          }
          catch (IOException ioe) {}
        }
      }
    
           public void showCamera(){
            try{
                player = Manager.createPlayer("capture://video");
                player.realize();
                videoControl = (VideoControl)player.getControl("VideoControl");
                canvas = new VideoCanvas(this, videoControl);
                canvas.addCommand(backCommand);
                canvas.addCommand(capture);
                canvas.setCommandListener(this);
                display.setCurrent(canvas);
                player.start();
            } catch (IOException ioe) {} catch (MediaException me) {}
        }
    
    class Video extends Thread {
            JudgeMidlet midlet;
            public Video(JudgeMidlet midlet) {
                this.midlet = midlet;
            }
    
            public void run() {
                captureVideo();
            }
    
            public void captureVideo() {
                try {
                    byte[] photo = videoControl.getSnapshot(null);
                    Image image = Image.createImage(photo, 0, photo.length);
                    form = new Form("Tag Your Photo");
                    form.append(image);
                    display.setCurrent(form);
                    player.close();
                    tags();
                    player = null;
                    videoControl = null;
                } catch (MediaException me) { }
            }
        }
    }
    
    class VideoCanvas extends Canvas {
        private JudgeMidlet midlet;
    
        public VideoCanvas(JudgeMidlet midlet, VideoControl videoControl) {
            int width = getWidth();
            int height = getHeight();
            this.midlet = midlet;
    
            videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
            try {
                videoControl.setDisplayLocation(2, 2);
                videoControl.setDisplaySize(width - 4, height - 4);
            } catch (MediaException me) {}
            videoControl.setVisible(true);
        }
    
        public void paint(Graphics g) {
            int width = getWidth();
            int height = getHeight();
    
            g.setColor(255, 0, 0);
            g.drawRect(0, 0, width - 1, height - 1);
            g.drawRect(1, 1, width - 3, height - 3);
        }
      }
    Any help would be much appreciated.

    Samii x

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    65
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Nevermind, i fixed it :P


  •  

    Posting Permissions

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