Home arrow Java arrow Page 2 - PHP Interacting with J2ME
JAVA

PHP Interacting with J2ME


Did you know that you can use PHP to interact with J2ME to work with mobile devices? If you didn't, read on to learn how...

Author Info:
By: Jason Lam
Rating: 5 stars5 stars5 stars5 stars5 stars / 95
June 10, 2003
TABLE OF CONTENTS:
  1. · PHP Interacting with J2ME
  2. · The Article
  3. · Conclusion

print this article
SEARCH DEVARTICLES

PHP Interacting with J2ME - The Article
(Page 2 of 3 )

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.

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.

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. 

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

  • kSOAP
  • kXML

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials