HTTP and J2ME

July 1, 2008 by ovidiu


            In this day and age, everything is connected to everything else. So let’s look at how your J2ME device can join the fun. More specifically let’s see how you can use HTTP and HTTPS from within your J2ME applications.

 

            Like all things Java, there are many ways one can accomplish this task. So while my solution might be valid, it is by no means the only one. So, let’s begin.

            Firstly, I would like to introduce you to the Connector class. It forms the basis of J2ME I/O. It is a factory singleton, meaning the Connector class has a static method named open() which, depending on the string argument supplied, creates the appropriate type of connection. Among others, it supports HTTP, HTTPS and socket connections.

            So, first we should create a HTTP connection and, based on that connection, create an InputStream.

 

HttpConnection hc = null;

hc =(HttpConnection) Connector.open(”http://www.halcyon-solutions.com/”);

InputStream is = hc.openInputStream();

 

            Reading HTTPS streams is no different. Provided the appropriate certificate is installed on the mobile device, all you have to do is change “http://” to  “https://”.

            Onwards. Now we have an InputStream associated with the HttpConnection object. Let’s say what we want to do next is get the entire contents of the stream inside a string object. One of the first solutions that comes to mind is:

 

String result = null;

DataInputStream dis = new DataInputStream (is);

result =dis.readUTF();

 

            However, if you try this, you’ll quickly encounter an EOFException. After a little digging, I’ve found an explanation:

 

“As it turns out, J2ME’s implementation of readUTF doesn’t treat a C-style NUL as a NUL — that is, contrary to the Unicode UTF-8 standard, it expects a NUL to be encoded as ‘\x0000′ (i.e. a full UTF16 atom). It happily just embeds a C-style NUL into the middle of a string, and keeps on reading and reading until it hits the end of a file and generates an EOFException.”

 

            There are several workarounds. For example, you can either read the stream byte-by-byte, or you can read it in chunks of bytes. For the sake of completeness, I’ll present both options.

 

First off, reading byte-by-byte:

 

StringBuffer sb = new StringBuffer();

int nextChar = 0;

while ( (nextChar = is.read()) != -1 )

{

    sb.append((char)nextChar);

}

 

System.out.println(sb);

 

 

Next, reading in chunks.

 

DataInputStream in = new DataInputStream(is);

int chunkSize = 512;

int offset = 0;

int readLength = 0;

byte [] chunk = new byte[chunkSize];

byte [] entireStream = new byte[chunkSize];

byte [] temp = null;

while ( (readLength = in.read(chunk, 0 , chunkSize)) != -1 )

{

    temp = entireStream;

    entireStream = new byte [offset+readLength];

    System.arraycopy(temp, 0, entireStream, 0, offset);

    System.arraycopy(chunk, 0, entireStream, offset, readLength);

    offset += readLength;

}

String str = new String(entireStream);

System.out.println(str);

 

            There are several thins to watch for when using HTTP and HTTPS connections. First, while in theory reading in chunks is faster, it may not always be the case. This is heavily platform dependent. Second, the number of simultaneous connections is also platform dependent. While some devices support quite a lot of simultaneous connections, it is not recommended to use more than two at a time. Note also that MIDP limits the number of HTTP connections open simultaneously to four.

 

You must be logged in to post a comment.