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) {
}
}
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';
Code: Select all
if ($_SERVER['REQUEST_METHOD'] == 'POST')
$str = trim(file_get_contents('php://input')); //get the raw POST data