Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 5
Registered: ‎04-19-2010
My Device: 9000
My Carrier: MTN

running a midlet on Blackberry simulator

Hi

 

I have written code for a Midlet that runs okay on Java Wireless Toolkit emulators but when i transfer the same code to eclipse, i cant run it in the Blackberry simulator. Does anyone know hoe i can accomplish this?

 

Thanks

Developer
Posts: 213
Registered: ‎07-18-2008
My Device: Not Specified

Re: running a midlet on Blackberry simulator

you have convert your MIDlet to a COD file to run it on your blackberry. Follow the instructions in this article on how to do this:

 

How To - Compile a MIDlet into a COD file

 

regards,

 

yosoh.

--------------------------------------------------
problemSolved() ? kudosPlease():kudosPlease();
New Contributor
Posts: 5
Registered: ‎04-19-2010
My Device: 9000
My Carrier: MTN

Re: running a midlet on Blackberry simulator

hi,

thanks for the link got some useful information. unfortunately with the blackberry jde installed in eclipse most of the sections it describes arent there. i managed to get the JAR imported to the project though and it run on the simulator. unfortunately the commands do not seem to be responding as they did with java wireless toolkit.

 

here is the sample code i used:

 

 

import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;


public class WhatsHot extends MIDlet implements CommandListener, Runnable{
	static final int DefaultTimeout = 2000;
    Display display;
	String url = "http://127.0.0.1/mobile/testGET.php";
    /** user interface alert component. */
    Alert splashScreenAlert;
    Image splashScreen;
	Form mainForm;
	 Ticker ticker = new Ticker(
    "Ads go here");
	Form data;
	Form data1;
	Form data2;
	Form data3;
	static final Command exitCommand = new Command("Exit", Command.STOP, 1);
	static final Command chkCommand = new Command("Check", Command.SCREEN, 2);
	static final Command bkCommand = new Command("Back", Command.SCREEN, 2);
    boolean imageLoaded;
	private Thread t = null;
	private boolean firstTime;
	ChoiceGroup CoursePOP;
	StringItem info;
	int o;
	String which;
	
	

    public WhatsHot() {
        display = Display.getDisplay(this);
		mainForm = new Form("Whats Hot");
		mainForm.setTicker(ticker);
		mainForm.addCommand(exitCommand);
		mainForm.addCommand(chkCommand);
		mainForm.setCommandListener(this);
		firstTime = true;
		CoursePOP = new ChoiceGroup ("Test", Choice.POPUP,new String[] {"Item1", "Item2", "Item3"}, null);
		mainForm.append(CoursePOP);
        
        
        try {
            splashScreen = Image.createImage("/sample/whatshot/screen.png");
            imageLoaded = true;
        } catch (java.io.IOException ex) {
        }

        splashScreenAlert = new Alert("Test", "", splashScreen, AlertType.INFO);
        splashScreenAlert.setTimeout(DefaultTimeout);     
    }
	
	 public void commandAction(Command c, Displayable s) {
		String label = c.getLabel();
		int n;
		if (label.equals("Exit")) {
			destroyApp(true);
		}else if (label.equals("Check")){
			for(int i=0;i<3;i++) {
			if(CoursePOP.isSelected(i)) {
				if(CoursePOP.getString(i) == "Item1"){
					which = "Item1";
					o = 0;
					if (t != null)
						return;
					t = new Thread(this);
					t.start();
				} else if (CoursePOP.getString(i) == "Item2"){
					which = "Item2";
					o = 1;
					if (t != null)
						return;
					t = new Thread(this);
					t.start();
				
				} else if (CoursePOP.getString(i) == "Item3"){
					which = "Item3 ";
					o = 2;
					if (t != null)
						return;
					t = new Thread(this);
				
				}
            }
		}
		}else if (label.equals("Back")){
			o = 0;
			display.setCurrent(mainForm);
		
		}
		
    }

    public void startApp() {
		if (firstTime) {
            display.setCurrent(splashScreenAlert, mainForm);
            firstTime = false;
        }	    
    }

    public void destroyApp(boolean unconditional) {
    }

    public void pauseApp() {
    }
	
	void testGET(String url) throws IOException {
        HttpConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        StringBuffer stringBuffer = new StringBuffer();
        StringItem txt;
		

        try {
          connection = (HttpConnection)Connector.open(url+"?type="+o);
          connection.setRequestMethod(HttpConnection.GET);
          connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
          connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.1");
          connection.setRequestProperty("Content-Language", "en-CA");
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          os = connection.openOutputStream();
          is = connection.openDataInputStream();
          long ch;
		 
          while ((ch = is.read()) != -1) {
			
            stringBuffer.append((char)ch);
			
          }
		  txt = new StringItem(which, stringBuffer.toString());
          data = new Form("Page Content");
		 
		  data.append(txt);
        } finally {
           if(is!= null) {
              is.close();
           }
           if(os != null) {
              os.close();
           }
           if(connection != null) {
              connection.close();
           }
        }
        display.setCurrent(data);
    }
	
	 public void run() {
	 try {
            testGET(url);
					} catch (IOException e) {
			System.out.println("IOException " + e);
			e.printStackTrace();
			}
	 
	 }
	

    
}