Beberapa jenis aplikasi mobile memerlukan konektivitas HTTP dan HTTPS. Jika perangkat bergerak tidak memiliki dukungan TCP/IP internal maka konektivitas tersebut disediakan operator melalui sebuah gateway. Mekanisme ini dipakai luas pada perangkat mobile yang tersedia dipasaran.menunjukkan aplikasi client‐server menggunakan HTTP.
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class formNilaiMidlet extends MIDlet implements CommandListener, Runnable{
private Command cmdExit, cmdHitung;
private Form form;
private Alert alert;
private TextField tfNama, tfNim, tfNilai1, tfNilai2, tfNilai3;
private static Display display;
private int errorcode = 0;
private String hasil;
public formNilaiMidlet(){
display = Display.getDisplay(this);
}
public void startApp() {
form = new Form("Hitung Nilai Mahasiswa");
tfNim = new TextField("NIM: ", null, 32, TextField.NUMERIC);
tfNama = new TextField("Nama: ", null, 32, TextField.ANY);
tfNilai1 = new TextField("Nilai 1", null, 32, TextField.NUMERIC);
tfNilai2 = new TextField("Nilai 2", null, 32, TextField.NUMERIC);
tfNilai3 = new TextField("Nilai 3", null, 32, TextField.NUMERIC);
cmdExit = new Command("Exit", Command.EXIT, 1);
cmdHitung = new Command("Hitung", Command.SCREEN, 1);
form.append(tfNim);
form.append(tfNama);
form.append(tfNilai1);
form.append(tfNilai2);
form.append(tfNilai3);
form.setCommandListener(this);
form.addCommand(cmdHitung);
form.addCommand(cmdExit);
display.setCurrent(form);
}
public void pauseApp() {
display.setCurrent(null);
}
public void destroyApp(boolean force) {}
public void commandAction(Command c, Displayable d){
if(c == cmdExit){
destroyApp(true);
notifyDestroyed();
}else if(c == cmdHitung){
new Thread(this).start();
}
}
public void run(){
try{
getData();
}catch(Exception e){}
}
public void getData() throws Exception{
String url = "";
url = "http://localhost:8080/formNilai/index.jsp";
HttpConnection hc = null;
InputStream is = null;
OutputStream os = null;
StringBuffer messagebuffer = new StringBuffer();
String temp = "";
try{
hc = (HttpConnection) Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
hc.setRequestProperty("Content-Language", "en-US");
hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os = hc.openOutputStream();
os.write(("nil1="+tfNilai1.getString()+"&nil2="+tfNilai2.getString()+"&nil3="+tfNilai3.getString()).getBytes());
os.close();
int rc = hc.getResponseCode();
if(rc != HttpConnection.HTTP_OK)
throw new IOException("HTTP response code : " + rc);
is = hc.openInputStream();
int ch;
long len = hc.getLength();
if((len!=-1)){
for(int i = 0;i<len;i++)
if((ch = is.read()) != -1){
messagebuffer.append((char)ch);
temp = temp + (char) ch;
}else{
while((ch = is.read()) != -1)
messagebuffer.append((char)ch);
temp = temp + (char) ch;
}
}
is.close();
//Parsing prs = new Parsing();
//prs.process(temp);
//String message = "Kode : " + prs.getCode() + "\nPesan :\n" + prs.getMsg();
//textbox.setString("Nama : Dian\nPassword : 9999");
//siHasil.setText(temp);
hasil = "NIM: "+ tfNim.getString() +"\nNama: "+ tfNama.getString() +"\nNilai Akhir: "+ temp;
alert = new Alert("Hasil");
alert.setTimeout(Alert.FOREVER);
alert.setString(hasil);
display.setCurrent(alert);
}catch (InterruptedIOException ei){
errorcode = -1;
throw ei;
}catch (IOException ioe){
errorcode = -2;
throw ioe;
}catch (Exception ie){
errorcode = -3;
throw ie;
}finally{
try{
if(hc != null) hc.close();
}catch (IOException ignored){}
try{
if(is != null) is.close();
}catch (IOException ignored){}
try{
if(os != null) os.close();
}catch (IOException ignored){}
}
}
}
Dan membuat satu file Http connection yang untuk menghubungkan antara keduanya.
untuk penghitungan nya di file jsp
contoh disini saya punya file: "http://localhost:8080/formNilai/index.jsp"
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String nilai1, nilai2, nilai3;
int nil1, nil2, nil3;
double total;
nilai1 = request.getParameter("nil1");
nilai2 = request.getParameter("nil2");
nilai3 = request.getParameter("nil3");
nil1 = Integer.parseInt(nilai1);
nil2 = Integer.parseInt(nilai2);
nil3 = Integer.parseInt(nilai3);
total = (nil1+nil2+nil3)/3;
out.println(total);
%>
Selamat mencoba.
By. http://edukasifirman.blogspot.com/
Komentar