J2ME Workshop - Take 1

Posted August 22nd, 2006 by Chris

This week, I’m conducting a small training workshop to introduce Wireless development to a few people. The course is designed to highlight some of the opportunities that wireless development allows, and how the J2ME platform can take advantage of those opportunities.

The basics introduced in the course are what I have learnt during and since the development of Toupix, and also help me to improve my knowledge further. I’m also using some of the frameworks developed for the course as part of ongoing research of mobile applications, the results of which I hope will grow into a full product for Pebbledot.

Talking of which, the commercial website is so close to launch now, just a few app issues to sort out with the online purchasing and licence generation for HostManager. I’m looking for some good Web-based project management software for HostManager 2.0’s development as I have decided to be a lot more organised for this major release!

Any suggestions welcome…

Posted in Business, HostManager, Personal | No Comments »

J2ME Uploading Images to PHP Script

Posted August 15th, 2006 by Chris

I’ve posted this as a helpful pointer to those looking into J2ME server interaction, and as a reminder to myself when I am next trying to figure out uploading J2ME images to PHP.

Something that constantly eluded me through the development of Toupix was an elegant way to get images from a phone to the server. Due to deadlines, I eventually decided to submit the image as an email and use PHP’s IMAP functionality to read and decode the email. Whilst functional, this wasn’t exactly the solution to end all solutions.

What frustrated me the most is that this should be something relatively simple, yet days and days of frustrating research and playing around with various encoding algorithms still resulted in invalid images being saved. The data was being received by the script, but something was getting garbled.

So, finally, I have discovered the solution - and it’s incredibly simple. Browsing forums, it is clear that the image data needs to be encoded into a Base64 string, and Stefan Haustein’s Base64 class provides the ideal method for doing this. Using Base64.encode(), the raw byte array of an image is returned as Base64 string.

public static boolean uploadImage( String uri, byte[] rawImage, String filename )
throws Exception
{
HttpConnectionhttpConnection;
OutputStream    out;

// Open connection to the script
httpConnection          = (HttpConnection)Connector.open( uri );

// Setup the request as an HTTP POST and encode with form data
httpConnection.setRequestMethod( HttpConnection.POST );
httpConnection.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" );

// Encode the imagedata with Base64
String    encoded    = Base64.encode( rawImage ).toString();

// Build the output and encoded string
String    output    = "file=" + filename + "&imgdata=" + encoded;

// Set the content length header
httpConnection.setRequestProperty("Content-Length", Integer.toString((output.getBytes().length)));

// Open the output stream and publish data
out = httpConnection.openOutputStream();
out.write( output.getBytes() );

// Flush the buffer (might not be necessary?)
out.flush();

// Here you might want to read a response from the POST to make
// sure everything went OK.

// Close everything down
if( out != null )
if( httpConnection != null )
httpConnection.close();

// All good
return true;
}

The next step is to get this string published to the server (PHP script). This is where the problems started. Most documentation I found assumed we are uploading to a JSP script, and there is little mention of PHP. The easiest way to get our data to PHP is via HTTP POST, and since our image is now a normal string, we can happily POST it to the server. The PHP script on the server can then read the string via the normal $_POST[] variable, and (theoretically) decode it:

...
//Decode the image from Base64 encoding
$img= base64_decode( $data );

//Write out data to file
if( $fp = fopen( $out, 'wb' ) )
{
//Write data to the file
fwrite( $fp, $img );

//Close the file
fclose( $fp );
}
else
{
die( "Error writing to image file $out" );
}
...

Unfortunately, though, something seems to happen to the data when it arrives on our server. It is at this point that everything was going wrong; the data was corrupt by the time it was decoded, and thus the image file produced was invalid. It turns out (and it had to be so simple) that we need to strip the Base64 string of spaces, and replace them with a + character!

function stripLineBreaks( $encode )
{
$data= str_replace( ' ', '+', $encode );
//$data= str_replace( 'rn', '', $data );
return $data;
}

Now, before decoding the base64 string, just run it through this function first:

...
//Decode the image from Base64 encoding
$data= stripLineBreaks( $data );
$img= base64_decode( $data );

//Write out data to file
...

At last, an image uploaded to your server via a J2ME MIDlet (under emulation - I’m using the Sony WTK set to use HTTP1.0). There are likely to be issues running it on a phone or emulator under HTTP1.1. My server doesn’t handle chunked data, which HTTP1.1 enforces by breaking the sent image data into chunks. This forum post shows a possible way around this, and if I find a successful method I’ll be sure to post it!

Was this post useful? Please leave me your comments and feedback.

Posted in Mobile, Programming | 4 Comments »

Plymouth Enterprise Week (Nov 13th - 18th)

Posted August 10th, 2006 by Chris

November 13th - 18th will see Plymouth hosting a number of events aimed at nurturing business enterprise in the City and South West region. There are several organisations involved in running the different events, including the University, local media and local business organisations.

It’s nice to see things happening in Plymouth to foster growth of local and regional businesses. As I seek to more firmly establish my own business (and secure a rather more formal business plan!), it’s good to know that the region is developing a vibrant and warm environment for new business types and organisations.

Business development and commercial work is, of course, all taking its toll on blogging frequency!

Posted in Uncategorized | No Comments »