Communication between J2ME client and PHP page in server


Sometimes it’s very necessary to communicate between a J2ME client and a PHP page in the server.
Here I have shown a J2ME program that communicate with PHP page in server.
I showed here HTTP GET method. But it is similar for POST method. I also showed the POST method’s syntax in this article. I choose php page because, PHP hosting is most popular.

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 .

/*
* Client.java
* Created on August 17, 2007, 11:42 AM
* @author  ahsan
* @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) {
    }
}

Now I have shown the PHP page that read the data from J2ME client and
display the necessary output to the J2ME client’s display:

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

Please use this syntax in the php page when using POST method both in J2ME client
and PHP page
:

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

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like

7 Comments

  • Md. Abdur Rahman
    June 14, 2008 at 12:07 pm

    assalamu alaikum.
    Nice to see your works as a PHP and J2ME expert. Will u mind if you ask for some technical help in this domain? I am waiting to hear from u.

  • Girish
    December 25, 2008 at 6:32 am

    Hello! Thanks for the example above.. It works great. However, I’m a bit confused for the POST method. Is it possible for you to send me a piece of code?
    Thanks.

  • bhanupriya
    May 6, 2010 at 1:00 pm

    hi, the code is simple and good.can u plz help me with code for sending large data from midlet to php?? waiting for quick response

  • mahmud ahsan
    mahmud ahsan
    May 6, 2010 at 1:04 pm

    @bhanupriya, I have not been working in j2me for last 2 years. I wish I could help you but I’m sorry. 🙁

  • mohammed
    March 14, 2012 at 1:17 am

    Thanks for the code. And it works correctly.

  • mai
    September 24, 2012 at 11:51 pm

    Thanks for the code 🙂

  • Admire
    February 8, 2014 at 1:20 pm

    Good day, I trust that this message will find you in a tip top condition, Kindly allow me to bother you for a second. I’m a university student and I have a school project I’m working on. I need to include Communication between mobile phone client and any server. Say I request a command using my phone and the server responds with a list of options to choose from. What I want is pretty much what you posted on http://thinkdiff.net/j2me/communication-between-j2me-client-and-php-page-in-server/. Will you be kind enough to email me the source code and I will try to implement something similar using Dot.NET because I’m not familiar with java or php. Thank you for responding.

Comments are closed here.

About Me

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

Flickr

Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed

Twitter Feed

Fanpage