Quantcast

PHP Interacting with J2ME

Get the WebProNews Newsletter:
[ Business]

You my have noticed in last year or two there has been an increasing boom in technology in the area of mobile handsets and mobile software development. Wireless technology is not only becoming more commonplace, but also more affordable. To support this even further, a survey released by IDC projected revenues in wireless gaming alone will generate approximately 72.2 million by 2007. Along side with mobile gaming there will be an ever-increasing demand for mobile applications.

This is all fine and dandy, but what does it have to do with PHP?  Not only will there be thousands of new wireless applications, but many existing applications of today will also be enhanced/modified to work with mobile handsets.  In most cases it would make sense to keep the current technology and architecture and simply have it interface with the new mobile technology.

This possibly means that sites already written in PHP now need to interact with mobile technology. One of the more popular emerging technologies is J2ME.  J2ME is provided by Sun Microsystems and stands for Java 2 Platform Micro Edition.  To learn more about J2ME and Java general, visit http://java.sun.com

J2ME produces small Java applications or games called midlets.  A very simple comparison would go like this: midlets are to WML what Java applets are to HTML.  Though midlets do not run directly inside WML, you would access/download a midlet through a WML capable browser.  You also have the option to directly upload the midlet to your mobile handset via a PC and cable connector.

There are a few options available when considering how to communicate from PHP to J2ME:

  • HTTP Simple Fetch
  • HTTP GET/POST
  • Web Services
  • Fetching Images

Requirements
This article uses both J2ME and PHP.  You will need the J2ME development kit from http://wireless.java.sun.com. You will also need a basic Apache and PHP web server setup.  Apache can be obtained from http://www.apache.org.  Please consult theses sites for installation instructions.  Once you’ve successfully installed the software, continue with the article.

Simple HTTP Fetch
Here is a simple example of fetching information from a text file:

J2ME Source Code:

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

public class SimpleFetch extends MIDlet {

    private Display display;
   
    String url = “
http://127.0.0.1/midlet/helloworld.txt“;

    public SimpleFetch() {
       display = Display.getDisplay(this);
    }
   
    public void startApp() {
 try {
            getViaStreamConnection(url);
 } catch (IOException e) {
            //Handle Exceptions any other way you like.
            System.out.println(“IOException ” + e);
            e.printStackTrace();
 }
    }
    public void pauseApp() {   }
    public void destroyApp(boolean unconditional) {  }

    /**
     * Read URL as Stream
     */
    void getViaStreamConnection(String url) throws IOException {
        StreamConnection streamConnection = null;
        InputStream inputStream = null;
        StringBuffer b = new StringBuffer();
        TextBox textBox = null;
        try {
          streamConnection = (StreamConnection)Connector.open(url);
          inputStream = streamConnection.openInputStream();
          int ch;
          while((ch = inputStream.read()) != -1) {
             b.append((char) ch);
          }         
          textBox = new TextBox(“Simple URL Fetch”, b.toString(), 1024, 0);
        } finally {
           if(inputStream != null) {
              inputStream.close();
           }
           if(streamConnection != null) {
              streamConnection.close();
           }
        }       
        display.setCurrent(textBox);
    }
}

PHP Source Code

In this case there really isn’t any source code, only a text file with the following contents:

Hello World!
by Jason Lam

The name of the text file as indicated in the J2ME code is helloworld.txt.

Output

Simple HTTP GET
Here is an example invoking PHP with GET parameters:

J2ME Source Code:

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

public class SimpleGETExample extends MIDlet {

    private Display display;

    String url = “http://127.0.0.1/midlet/testGET.php?type=2“;

    public SimpleGETExample() {
       display = Display.getDisplay(this);
    }

    public void startApp() {
 try {
            testGET(url);
 } catch (IOException e) {
     System.out.println(“IOException ” + e);
     e.printStackTrace();
 }
    }

    public void pauseApp() {    }
    public void destroyApp(boolean unconditional) {   }

    void testGET(String url) throws IOException {
        HttpConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        StringBuffer stringBuffer = new StringBuffer();
        TextBox textBox = null;

        try {
          connection = (HttpConnection)Connector.open(url);
          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.0″);
          connection.setRequestProperty(“Content-Language”, “en-CA”);
          connection.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);
          os = connection.openOutputStream();
          is = connection.openDataInputStream();
          int ch;
          while ((ch = is.read()) != -1) {
            stringBuffer.append((char) ch);
          }
          textBox = new TextBox(“Simple GET Test”, stringBuffer.toString(), 1024, 0);
        } finally {
           if(is!= null) {
              is.close();
           }
           if(os != null) {
              os.close();
           }
           if(connection != null) {
              connection.close();
           }
        }
        display.setCurrent(textBox);
    }
}

PHP Source Code:

<?php
$response = “Hello”;

if (isset($_GET)) {
  switch ($_GET["type"]) {
    case 1: $response = “Good Morning”; break;
    case 2: $response = “Good Afternoon”; break;
    case 3: $response = “Good Evening”; break;
    default: $response = “Hello”; break;
  }  
}
echo $response;
?>

Output:



Depending on what the midlet sends to the testGET.php script in the GET parameter, different results are returned.  If you are already familiar with using GET and POST, you can see that there isn’t really much of difference it is quite trivial.

[Note] The results would be the same if you decided to make a POST call instead of a GET call. Of course, however, both the J2ME and PHP source code would have to be changed accordingly to handle a POST call instead of a GET call. [End Note]

Simple WebService
You probably know that you can provide web services with PHP and NuSoap.  One of the great advantages of a web service is the fact that it provides independence from operating systems and platforms.  To learn more about web services, refer to Ahm Asaduzzaman’s excellent article on Building XML Web Services with PHP NuSOAP published at http://www.devarticles.com/art/1/414.

In this example you will also need the appropriate libraries for the midlet to run correctly:

In this example we are using the 1.x versions of kSOAP and kXML.

J2ME Source:

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

import org.ksoap.*;
import org.ksoap.transport.*;
import org.ksoap.SoapObject;

public class SimpleWebService extends javax.microedition.midlet.MIDlet {   
  private Display display;       
  private String url = “
http://127.0.0.1/midlet/webservice/service.php“;
  TextBox textbox = null;
   
  public SimpleWebService() {
    display = Display.getDisplay(this);
  }

  public void startApp() {
    try { 
      testWebService();
    } catch (Exception ex) {
      System.out.println(ex);  
    }
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }
       
  public void testWebService() throws Exception {
    StringBuffer stringBuffer = new StringBuffer();
   
    TextBox textBox = null;
   
    // First WebService – echos name that is passed in, in this case ‘Jason’
    SoapObject client = new SoapObject(url,”hello”);
    client.addProperty(“name”,”Jason”);
    HttpTransport ht = new HttpTransport(url,”hello”); 
    stringBuffer.append(ht.call(client));

    // 2nd WebService – Supply 2 numbers and the result is the sum of the
    //                  two numbers
    client = new  SoapObject(url,”add”);
    client.addProperty(“x”,”7″);
    client.addProperty(“y”,”6″);
    ht = new HttpTransport(url,”add”);
    stringBuffer.append(“nAdd Result: ” + ht.call(client));     
   
    // display results in textbox
    textBox = new TextBox(“Simple WebService Test”, stringBuffer.toString(), 1024, 0);
    display.setCurrent(textBox);
  }
}

PHP Source Code:

<?php
// include NuSOAP library
require_once(‘nusoap.php’);

// Create Web Service Server
$server = new soap_server; 

// Register Services
$server->register(‘hello’);
$server->register(‘add’);

// Define Services
function hello ($name){
  return “Hello $name”;

function add ($x,$y){
  return $x + $y;

$server->service($HTTP_RAW_POST_DATA);
?>

Output:



Was that easy or what?  The beauty of this is you can now quite easily interface your new PHP web service(s) with any device, whether it be WebTV/iTV, an SIP device, or even a mobile device running something other than J2ME (such as Symbian, PalmOS, MoPhun, Brew, MS SmartPhone).

Simple Image Fetch
You can even retrieve binary data such as images.  If the mobile device is capable of view documents like word or PDF then you should be able to send those by HTTP as well.

J2ME Source Code:

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

public class SimpleImageFetch extends MIDlet
{
  private Display display;
  private String URL = “
http://127.0.0.1/midlet/image/test.php“;
  private Form formImage;

  public SimpleImageFetch()
  {
    try {
      display = Display.getDisplay(this);
      Image im = getImage(URL);
      formImage = new Form(“Simple Image Test”);
      formImage.append(im);
      display.setCurrent(formImage);
    } catch (Exception ex) {
      System.out.println(ex);
    }

}

  public void startApp() { }
  public void pauseApp() { }
  public void destroyApp(boolean unconditional) {  }

  private Image getImage(String url) throws IOException
  {
    ContentConnection connection = (ContentConnection) Connector.open(url);
    DataInputStream iStrm = connection.openDataInputStream();
    Image im = null;

    try {
      byte imageData[];
      ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
      int ch;
      while ((ch = iStrm.read()) != -1)
        bStrm.write(ch);
      imageData = bStrm.toByteArray();
      bStrm.close();
      im = Image.createImage(imageData, 0, imageData.length);
    } finally {
      if (iStrm != null)
        iStrm.close();
      if (connection != null)
        connection.close();
    }
    return (im == null ? null : im);
  }
}

PHP Source Code:

<?php
$filename = “./phpjava.png”;
$handle = fopen ($filename, “rb”);
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
header(“Content-type: image/gif”);
header(“Content-length: “.(string)(filesize($filename)));
echo $contents
?>

Output:

Beyond Basic HTTP Communication
In general, we’ve only dealt with basic HTTP communications.  There are other methods of communication you may want to consider as well when using PHP, such as SMS, email or sockets.

Remember that the examples shown in this article are very simplistic and are meant for demonstration purpose only.  For more production ready code, you should be aware of:

  • The way J2ME handles Session and Cookies
  • J2ME and PHP page caching
  • Detection of HTTP_USER_AGENT
  • Encryption, Base64, MD5, Bouncy Castle ( http://www.bouncycastle.org ) with MIDP 2 comes with PKI support
  • Latency,
  • Size data being transferred,
  • Frequency of data being transferred

The list above shows things to beware of and is really only related to the communication side of things.  There are other considerations that are out of the scope of this article such as performance, data persistence and porting constraints to name a few.

Conclusion

Summary
The goal of this article was to introduce several ways of interacting PHP with J2ME.  In the J2ME world there are dozens of tutorials that already show network examples between J2ME and JSP/Servlets and briefly mention that it is possible to interact with other server-side languages/scripts.

On the flip side of things, there are dozens of examples of different ways PHP can interact with other PHP pages on different servers, while briefly stating that it’s possible to make interact PHP with other non-PHP scripts as well.  Hopefully this article has demonstrated that this is possible and that two different types of technologies can indeed work together. 

Some may argue, why mix the 2 types of languages in the first place?  Doesn’t it make sense just to use JSP/Servlets with J2ME?  Well yes, I have to agree, if you are going to start a project from scratch (as mentioned above), then this situation is most likely to arise when supporting already existing systems where the existing system may already be written in PHP and not JSP/Servlets.  You should always keep an open mind when using technology. Given the circumstances, you should always use what is best for each situation.


First published at DevArticles

Jason is a wireless and open source developer enthusiast who enjoys creating synergy and sharing knowledge in the software development world. To learn more about him visit his personal site at http://www.jasonlam604.com/

PHP Interacting with J2ME
About Jason Lam
Jason is a wireless and open source developer enthusiast who enjoys creating synergy and sharing knowledge in the software development world. To learn more about him visit his personal site at http://www.jasonlam604.com/ WebProNews Writer
Top Rated White Papers and Resources
  • Guest

    good