J2ME & PHP
J2ME merupakan bahasa bahasa pemrograman java yang khusus diperuntukan untuk aplikasi wireless/mobile. Umumnya, J2ME sering dikombinasikan dengan saudaranya yaitu J2EE tetapi bukan tidak mungkin kalau J2ME dikombinasikan denfan PHP. Pada artikel kali ini penulis berusaha menyajikan sedikit contoh bagaimana J2ME berinteraksi dengan PHP. Berikut adalah contoh skripnya:
Kode J2ME :
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class SimpleGETExample extends MIDlet {
private Display display; String url = "http://127.0.0.1/midlet/testGET.php?type=2";
public SimpleGETExample() {
display = Display.getDisplay(this);
}
public void startApp() {
try {
testGET(url);
} catch (IOException e) {
System.out.println("IOException " + e);
e.printStackTrace(); }
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
void testGET(String url) throws IOException {
HttpConnection connection = null;
InputStream is = null;
OutputStream os = null;
StringBuffer stringBuffer = new StringBuffer();
TextBox textBox = null;
try {
connection = (HttpConnection)Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-CA");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os = connection.openOutputStream();
is = connection.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
stringBuffer.append((char) ch);
}
textBox = new TextBox("Simple GET Test", stringBuffer.toString(), 1024, 0);
} finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(connection != null) {
connection.close();
}
}
display.setCurrent(textBox);
}
}
Kode PHP :
case 1: $response = "Good Morning"; break;
case 2: $response = "Good Afternoon"; break;
case 3: $response = "Good Evening"; break;
default: $response = "Hello"; break; }
}
echo $response;?>
Posting Komentar