Diberdayakan oleh Blogger.
RSS

J2ME

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;
import java.io.*;
public class UjianB8 extends MIDlet{
public FormAwal fAwal;
public gambarCanvas gambar;
public eReMeS rms;
public Ticker ticker;
public UjianB8(){
fAwal = new FormAwal(this);
gambar = new gambarCanvas(this);
rms = new eReMeS(this);
ticker = new Ticker("Radiansah Eka Sopanji");
fAwal.setTicker(ticker);
}
public void startApp(){
Display.getDisplay(this).setCurrent(fAwal);
}
public void pauseApp(){
}
public void destroyApp(boolean b){
notifyDestroyed();
}
}
class FormAwal extends Form implements CommandListener{
UjianB8 app;
public FormAwal(UjianB8 app){
super("");
this.app= app;
append("Nama : Radiansah Eka Sopanji ");
append("NPM : 55411735 ");
append("Kelas : 2ia20 ");
append("Materi : J2ME ");
append("Universitas Gunadarma ");
addCommand(new Command("Keluar", Command.EXIT,0));
addCommand(new Command("Selanjutnya", Command.OK,1));
setCommandListener(this);
}
public void commandAction(Command c, Displayable d){
switch(c.getCommandType()) {
case Command.OK:
Display.getDisplay(app).setCurrent(app.gambar);
break;
case Command.EXIT:
app.destroyApp(true);
}
}
}
class gambarCanvas extends Canvas implements CommandListener{
UjianB8 app;
public void paint(Graphics g){
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xffffff);
g.drawString("KURSUS MIDLET", 80,70,g.LEFT|g.TOP);
Image img = null;
try{
img = Image.createImage("/crayon.png");
}catch(Exception e){}
g.drawImage(img,getWidth()/2,getHeight()/2,g.HCENTER|g.VCENTER);
}
public gambarCanvas(UjianB8 app){
this.app= app;
addCommand(new Command("Kembali", Command.EXIT,0));
addCommand(new Command("Selanjutnya", Command.OK,1));
setCommandListener(this);
}
public void commandAction(Command c, Displayable d){
switch(c.getCommandType()) {
case Command.OK:
Display.getDisplay(app).setCurrent(app.rms);
break;
case Command.EXIT:
Display.getDisplay(app).setCurrent(app.fAwal);
}
}
}
class eReMeS extends Form implements CommandListener{
UjianB8 app;
TextField nama;
Alert alert;
Form hasil;
RecordStore rs = null;
public eReMeS(UjianB8 app){
super("");
this.app= app;
nama = new TextField("Masukkan nama : ", null, 25, TextField.ANY);
hasil = new Form("Hasil:");
hasil.addCommand(new Command("OK", Command.OK, 1));
hasil.addCommand(new Command("Keluar", Command.EXIT, 1));
hasil.setCommandListener(this);
append(nama);
addCommand(new Command("Kembali", Command.EXIT,0));
addCommand(new Command("Simpan", Command.OK,1));
setCommandListener(this);
}
public void commandAction(Command c, Displayable d){
switch(c.getCommandType()) {
case Command.OK:
tambahData(nama.getString());
try {
new Thread(new Runnable() {
public void run() {
try {
if (hasil.size() > 0) hasil.delete(0);
hasil.append( HTTPManager.postData(encode(nama.getString())));
alert = new Alert("Success",""+(append( HTTPManager.postData(encode(nama.getString())))), null , AlertType.INFO);
} catch (Exception e) {}
}
}).start();
} catch (Exception e) {}
//Display.getDisplay(app).setCurrent(hasil);
alert = new Alert("Success","Nama Anda : " +nama.getString(), null , AlertType.INFO);
//alert = new Alert("Success",""+(append( HTTPManager.postData(encode(nama.getString())))), null , AlertType.INFO);
//Display.getDisplay(app).setCurrent(alert);
nama.setString("");
tampilHasil();
return;
case Command.EXIT:
Display.getDisplay(app).setCurrent(app.gambar);
}
}
public void tambahData(String s){
try{
byte[] rec;
rec = s.getBytes();
rs.addRecord(rec,0,rec.length);
}
catch(Exception e){}
}
public void bacaData(){
RecordEnumeration re = null;
byte [] rec;
String s;
int rec_id;
try{
re = rs.enumerateRecords(null,null, false);
while(re.hasNextElement()){
rec_id = re.nextRecordId();
rec = rs.getRecord(rec_id);
s = new String(rec);
hasil.append("#" + rec_id + " : " + s + "\n");
}
}catch(Exception e){}
}
public void tampilHasil(){
while(hasil.size() > 0)hasil.delete(0);
bacaData();
Display.getDisplay(app).setCurrent(hasil);
}
public void openRMS(){
try{
rs = RecordStore.openRecordStore("MyRMS",true);
}catch(Exception e){
}
}
public void closeRMS(){
try{
rs.closeRecordStore();
}
catch(Exception e){}
}
public String encode(String s) {
if (s == null) return s;
String res="";
char ch;
for (int i=0; i<s.length(); i++) {
ch = s.charAt(i);
switch (ch) {
case '-':
case '_':
case '@':
case '.':
res += ch;
break;
case ' ':
res += '+';
break;
case '\n':
res += "%0A";
break;
default:
if ( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9') )
res += ch;
else
if (ch<256) res += "%" + toHex(ch);
else res +="%26%23"+ (int) ch + "%3b";
}
}
return res;
}
public String toHex(char ch) {
String res = Integer.toHexString((int) ch);
if (res.length() == 1) res = '0' + res;
return res;
}
}
class HTTPManager {

public static String postData (String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
String hasil = null;
try {
c=(HttpConnection)Connector.open("http://localhost/Ujidata/index.php?nama=" +url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
int status = c.getResponseCode();
if (status!=HttpConnection.HTTP_OK) {
hasil = String.valueOf(status);
return hasil;
}
is = c.openInputStream();
int len = (int)c.getLength();
if (len>0){
byte [] data = new byte[len];
int actual = is.read(data);
hasil = new String (data);
} else {
int ch;
hasil = "";
while ((ch = is.read()) !=-1){
hasil = hasil + (char) ch;
}
}
} finally {
if (is!=null) is.close();
if (c !=null) c.close();
}
return hasil;
}
}

Output


  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 komentar:

Posting Komentar