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.
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;
// 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.
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 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.