Archive for the ‘Java’ Category

Derek Santos

Hello World with the BlackBerry Mobile Platform

Saturday, May 1st, 2010

By Derek Santos – Consultant

The Mobile Market is extremely competitive these days, and it’s important for customers to deploy their applications across multiple mobile platforms. The BlackBerry platform, like Android, is Java based. This lends itself nicely to existing Java and/or Android developers.  In this article, we’ll take a look at creating a HelloWorld application using the BlackBerry Mobile Platform.

Downloading and installing the necessary tools

First, you will need to download the BlackBerry Eclipse Plug-in here.

It is important to note that versions of the BlackBerry platform 4.x and earlier are considered legacy and use what’s called the “BlackBerry JDE(Java Development Environment)”. Versions 5.0 and later now use the BlackBerry Eclipse Plug-in. This article assumes you have Eclipse 3.5 installed.

Once you’ve downloaded the installer, simply run it and point it to your existing eclipse installation.

Creating your first BlackBerry project

To create your project, go to New -> Project.

Then select BlackBerry Project.
BlackBerry Project Selection

Click “Next”. Give your project a name then click “Finish”.

Configuring your project

You now have a BlackBerry project setup. You should notice that an application descriptor file was created for you. This descriptor file contains the Meta information about your project. It can be found under the root of your project.
BlackBerry Application Descriptor

Open this file and enter in information about your project.

BlackBerry Descriptor Configuration

Creating your main application class

Now that your project is configured, you’ll want to create a new Java class and have it extend UIApplication. The UIApplication class is your main entry point into the application. This class will also have your static main() function.

BlackBerry Descriptor Configuration

Once you’ve created your class, add the code to your static main() method as shown below. The enterEventDispatcher() method indicates that your main application thread will execute any drawing and event-handling code.

public static void main(String[] args) {
    HelloWorld application = new HelloWorld();
    application.enterEventDispatcher();
}

At this point, you will have an empty BlackBerry Application.

Creating your applications main screen

BlackBerry applications draw screens on a stack. To initialize this stack, we need to create a class that extends the “MainScreen” class. Create a new Java class and configure it as seen below.

BlackBerry Descriptor Configuration

After creating the class, create a default constructor and add the following code to it.

public SampleMainScreen() {
    super();
    long options = LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH;
   
    LabelField title = new LabelField("HelloWorld Sample", options);
    setTitle(title);

    RichTextField richTextField = new RichTextField("Hello World!");
    add(richTextField);
}

Now that you’ve created your main screen, you are ready to add it to your application.

Adding your main screen to the application

To have your application display your main screen, you need to “push” the screen onto the applications stack. To do this, add this line of code to the HelloWorld applications constructor. Also make sure you are calling super().

public HelloWorld() {
    super();
    pushScreen(new SampleMainScreen());
}

Running your application

To run your BlackBerry application, simply click the Run dropdown, go to Run As -> BlackBerry Simulator.

Run As BlackBerry Simulator

The first run will take some time as the Simulator initializes. Once the Simulator is loaded up, you need to navigate to the application. You can do this by click the BlackBerry menu button then going to Downloads -> My First Application.

BlackBerry Downloads Folder

Once you click on “My First Application”, your should see “HelloWorld” printed on the screen.

Conclusion

This concludes creating a simple application using the BlackBerry Mobile Platform. For further reading, please see the BlackBerry Developer Zone.

Thanks for reading!

Derek Santos

XMLSocket Benchmarking with Flex and Java

Wednesday, January 13th, 2010

By: Derek Santos

Socket driven applications have several benefits. The benefits include real-time data interchange between the Client and Server, and giving the developer more control over how the data is sent.  This post will cover some basics about sending xml data over sockets, and how the socket performs with large data sets.

Setting Up a Socket Server

The first thing you need to do is create a socket server.  This can be done with Java via the ServerSocket class. Please refer to the attachments to see how this is done. In this example, we will be connecting to a socket on port 5678.  It’s important to note that since Flash Player 9.0.124.0, a policy file is required to be served by the socket.  You can serve a policy file on port 843, which will allow any subsequent socket connections for any port on that host. A Policy File server is also provided in the attachments. In this example, the Java Socket Server simply responds to a request and returns “n” number of xml rows back to the client. The Socket Server will return the data in intervals, rather than all at once. The purpose of this example is to determine at what interval you can achieve the best performance.

Below is the xml we will be expecting from the server.

<person>
    <id>1</id>
    <name>Derek Santos</name>
    <company>New Toronto Group</company>
</person>

Once our Socket Server is up, we can make requests to the server from the Client.  To do this, you can use the XMLSocket class.

var socket:XMLSocket = new XMLSocket();
socket.connect(‘localhost’, 5678);

This will an attempt to connect to the socket server we have created.  We also need to listen for the connect event of the socket, so that we can begin sending requests.

socket.addEventListener(Event.CONNECT, connectHandler);
private function connectHandler(event:Event):void {
socket.send(“my first socket request”);
}

Although it is an XMLSocket, you are not required to send data in XML format.  In fact, the server does not know the difference between an xml socket and a regular socket connection.  You can handle results from the server by adding an event listener on the socket for the DataEvent.

socket.addEventListener(DataEvent.DATA, dataHandler);

XMLSocket Test Results

The screenshot below shows a simple Flex application which asks the server for XML data.  First it asks for 10,000 rows, then 100,000 and then 1,000,000 rows.  The chart below represents the time it took to retrieve all of the rows(y axis), and how many rows at a time were returned(x axis).

Benchmarking Results

This test was run on dual-core processor with 2GB of ram.  Interestingly, it looks like the optimal setting is around 32 rows per result.

You can download all of the source for this example below.

Download Source Code