How to interact J2ME with PHP

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to interact J2ME with PHP

Post by Saman » Tue Sep 13, 2011 3:33 pm

Sometimes it’s very necessary to communicate between a J2ME client and a PHP page in the server. Here is a J2ME program that communicate with PHP page in server. This shows HTTP GET method. In a similar way we can work out POST method. Here you will see syntax of POST method as well. Since PHP hosting is very popular, we used php here.

Here is the J2ME Program:
This program get a number from user and send this to the php page in server. Server read and
send necessary output as a return .

Code: Select all

/*
* Client.java
* @version
*/
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
 
public class Client extends MIDlet implements CommandListener {
private Display display;
private Form form;
private Command cQuit, cOk;
private String url = 'http://yourphppage.php?type=';
private String part='';
private TextField f;
 
HttpConnection http;
InputStream in;
OutputStream out;
int rc;
 
public void startApp() {
    display = Display.getDisplay(this);
    form = new Form('Client');
    cQuit = new Command('Quit', Command.EXIT, 1);
    cOk = new Command('OK', Command.OK, 1);
 
    f = new TextField('Query', '',10, TextField.NUMERIC);
 
    form.addCommand(cQuit);
    form.addCommand(cOk);
    form.setCommandListener(this);
    form.append(f);
 
    display.setCurrent(form);
}
 
public void processGet() throws Exception{
    http = (HttpConnection) Connector.open(url+part);
    http.setRequestMethod(HttpConnection.GET);
    http.setRequestProperty('IF-Mofified-Since', '10 Nov 2006 17:29:12 GMT');
    http.setRequestProperty('User-Agent', 'Profile/MIDP-2.0 Configuration/CLDC-1.1');
    http.setRequestProperty('Content-Language', 'en-US');
 
    in = http.openDataInputStream();
    out = http.openDataOutputStream();
 
    rc = http.getResponseCode();
    if (rc != HttpConnection.HTTP_OK) {
        throw new IOException('HTTP response code: ' + rc);
    }
 
    int ch;
    StringBuffer buff = new StringBuffer();
    while  ( (ch = in.read())!= -1){
    buff.append( (char) ch);
    }
    form.append(new StringItem('Response: ', buff.toString()));
 
    if (in != null)
        in.close();
    if (out != null)
        out.close();
    if (http != null)
        http.close();
}
 
public void commandAction(Command com, Displayable d){
    if (com == cQuit){
        destroyApp(true);
        notifyDestroyed();
    }
    else if (com == cOk){
        part = f.getString().trim();
        try{
            processGet();
        }
        catch(Exception o){
            o.printStackTrace();
        }
    }
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
}
The PHP page that reads data from J2ME client and display the necessary output to the J2ME client’s display:

Code: Select all

//hello.php
$response = 'Hello, every body';
 
if (isset($_GET)){
    switch($_GET['type']){
        case 1: $response = 'Good Moring'; break;
        case 2: $response = 'Good evening '; break;
        case 3: $response = 'Visit: www.ftechdb.com'; break;
        default: $response = 'Hi to all' ;
    }
}
echo '$response';
Use this syntax in the php page when using POST method both in J2ME client and PHP page:

Code: Select all

if ($_SERVER['REQUEST_METHOD'] == 'POST')
$str = trim(file_get_contents('php://input')); //get the raw POST data
Post Reply

Return to “PHP & MySQL”