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 server 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 .

* Created on August 17, 2007, 11:42 AM

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


* @author ahsan
* @version
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);



public void processGet() throws Exception{
http = (HttpConnection);
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 =!= -1){
buff.append( (char) ch);
form.append(new StringItem("Response: ", buff.toString()));

if (in != null)
if (out != null)
if (http != null)


public void commandAction(Command com, Displayable d){
if (com == cQuit){
else if (com == cOk){
part = f.getString().trim();
catch(Exception o){

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:


$response = "Hello, every body";

if (isset($_GET)){
case 1: $response = "Good Moring"; break;
case 2: $response = "Good evening "; break;
case 3: $response = "Visit:"; 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

$str = trim(file_get_contents('php://input')); //get the raw POST data


19 Responses to “Communication between J2ME client and PHP page in server”

  1. Md. Abdur Rahman Says:

    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.

  2. Girish Says:

    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?

  3. leena Says:

    plz tell me the code for transmitting image from server and saving it to server using j2me and php…

  4. youcef Says:

    please help me to my project ; is to do a client (etudient) who acces to server for seeing his notes by j2me,thank you all.

  5. tosha Says:

    Nice job this would be a great help to me for as i m connecting mysql and j2me via server side language as php as its most flexible….. 🙂 thanks

  6. tosha Says:

    i m wondering if you can send me the code for post method my mail id is….

  7. Aqdas Says:

    good Article

  8. jithin Says:

    Good one
    thank u

  9. NitNush Says:

    Good work…but to avoid deadlock, allocate a separate worker thread for the http connection…otherwise the gui will become unresponsive

    here’s the modification:

    public void processGet() throws Exception{

    // Create a thread to actually connect to the webserver
    Thread t = new Thread()
    public void run()
    try {
    http = (HttpConnection);


  10. Adnan Says:

    great Article

  11. Sam Says:

    Hi Great Job, am doing a research on remote monitoring using j2me as the client is a mobile device, i will be thankful if u show me how can u access a file through j2me?
    Much appreciated!

  12. Misteryus Says:

    What is deferent between InputStream and dataInputStream, iam still confious with that.Thanks before

  13. Leanna Says:

    This post, “Communication between J2ME client and PHP page in server
    | Think Different” was excellent. I’m making out a copy
    to show my personal pals. I appreciate it,Delila

  14. Chastity Says:

    “Communication between J2ME client and PHP page in server | Think Different”
    was indeed a beneficial post. If solely there was significantly more weblogs like this specific one on the actual world wide web.
    Well, thanks for your personal precious time, Pamela

  15. Asa Says:

    I personally speculate exactly why you titled this specific post,
    “Communication between J2ME client and PHP page
    in server | Think Different”. In any event I actually
    admired the post!Thanks for the post,Seth

  16. Eugenio Says:

    “Communication between J2ME client and PHP page in server | Think Different” really enables me imagine a little bit more.
    I adored every single portion of it. Thanks for your effort -Tawanna

  17. Mariano Says:

    “Communication between J2ME client and PHP page in server | Think Different” was indeed a truly nice
    blog post, . I hope you keep publishing and I’m going to continue to keep browsing! Thanks a lot ,Lance

  18. free Says:

    I am really grateful to the holder of this web page who has
    shared this great paragraph at at this place.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: