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 .
/*
* Client.java
*
* Created on August 17, 2007, 11:42 AM
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
/**
*
* @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);
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
<?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
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.
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.
March 4, 2009 at 9:03 am
plz tell me the code for transmitting image from server and saving it to server using j2me and php…
April 19, 2009 at 11:22 pm
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.
April 19, 2009 at 11:23 pm
this my e-mail ; youcef.cherif@gmail.com
May 8, 2009 at 3:53 pm
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
May 8, 2009 at 3:54 pm
i m wondering if you can send me the code for post method my mail id is tosha9@gmail.com….