Table of ContentsNetwork GamingThe Server Side

A Simple Networked MIDlet

HTTP communications from a MIDlet are carried out using the generic connection framework. This system uses a single "superconnector" factory for any type of supported connection. To make a connection to an HTTP server you simply open the URL and then read the response. In the following MIDlet example you'll create a connection to the java.sun.com Web site, download the homepage, and display the first few hundred bytes. You can build and run this example as a standalone MIDlet.

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

public class NetworkingTest extends javax.microedition.midlet.MIDlet
{
    private Form form;

    public NetworkingTest() throws IOException
    {
        // Set up the UI
        form = new Form("Http Dump");
    }

    protected void pauseApp()
    {
    }  
protected void startApp() throws MIDletStateChangeException
{
    // display our UI
    Display.getDisplay(this).setCurrent(form);
    communicate();
}

protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
}

    private void communicate()
    {
        try
        {
            // Create an HTTP connection to the java.sun.com site
            InputStream inStream = Connector.openInputStream("http://java.sun.com/");

            // Open the result and stream the first chunk into a byte buffer
            byte[] buffer = new byte[255];
            int bytesRead = inStream.read(buffer);
            if (bytesRead > 0)
            {
                inStream.close();

                // Turn the result into a string and display it
                String webString = new String(buffer, 0, bytesRead);
                System.out.println(webString);
                form.append(webString);
            }
        }
        catch(IOException io)
        { }
    }
}

This code shows the basic structure of communications between a MIDlet (the HTTP client) and an HTTP server. Create a connection, request a URL, and read back the response from the server. It's a simple transaction-style communications mechanism.

However, to use this in a game you need to do things a little differently. The first issue is the time it can take for communications to occur. Because of this delay you can't have the MIDlet remain unresponsive to the user. At the very least, you need to provide a command to cancel out; even better, you could provide them with some feedback that stuff is happening. To do this you need to do your communications within a new thread. For example:

// create a new thread for the networking (inline class)
Thread t = new Thread()
{
    // declare a run method - not called until the thread is started
    // (see t.start below)
    public void run()
    {
        communicate();
    }
};

// start the thread (execute the contents of the run method above)
t.start();

NOTE

Tip

Watch out for some of the Nokia emulators when you are doing network programming. Many of the older versions (including the 7210 v1.0 emulator) have a bug that shows up when you do multi-threaded HTTP requests while updating the display. The bug will crash your MID and output a Conflicting Stack Sizes error. If you encounter this, consider using a newer emulator in the same series. The 3300, for example, does not exhibit this behavior.

Now that you have some basic client communications working, take a look at the other side of the equationthe server.

    Table of ContentsNetwork GamingThe Server Side