Home > J2ME, Java, RMS > Simple Class for HTTP Connection in Java Micro Edition

Simple Class for HTTP Connection in Java Micro Edition


package network;

import java.io.InputStream;
import java.io.IOException;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.*;

public abstract class HTTPSend extends Form implements Runnable,CommandListener{
    String HTTPErrMsg="",HTTPResponse="",URL="";
    Gauge gg=new Gauge("Koneksi Ke Server..",false,10,0);
    Command bck=new Command("Kembali",Command.BACK,0);

    Display dis;
    Displayable bckscreen;
    Thread tr;
    public HTTPSend(Display display){
        super("Kirim Data");
        dis=display;
        bckscreen=dis.getCurrent();
        append(gg);      
        append("\n Silahkan Tunggu..");
        setCommandListener(this);
    }

    public void SendData(String urltarget){
       URL=urltarget;
       tr=new Thread(this);
       tr.start();
    }

    public Thread getTr() {
        return tr;
    }

    public void setTr(Thread tr) {
        this.tr = tr;
    }
    
    private String ReadResponse(HttpConnection cn){
        int ch;
        InputStream inputStream = null;
        String tmp="";
        try {
            inputStream = cn.openInputStream();
            gg.setValue(5);
            StringBuffer stringBuffer = new StringBuffer();
            while((ch = inputStream.read()) != -1)     
                stringBuffer.append((char) ch);
            if(inputStream != null)     inputStream.close();        
            if(stringBuffer != null)    tmp= new String(stringBuffer); 
            gg.setValue(7);
        } catch (IOException ex) {
        }
        return tmp;
    }
    
   private boolean proseskirim(){
   boolean status=false;
	HttpConnection httpConnection = null;
        try {
            httpConnection = (HttpConnection) Connector.open(URL);
            gg.setValue(1);
            httpConnection.setRequestMethod(HttpConnection.GET);
            gg.setValue(3);
            if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK){
            	 HTTPResponse=ReadResponse(httpConnection);
                 status= true;
            }else{
                HTTPErrMsg="Request Tidak Dikenali  !";
            }
            if(httpConnection != null)  httpConnection.close();
            gg.setValue(9);
	}catch (Exception e) {
//            System.out.println("Kerusakan "+e.getMessage());
           HTTPErrMsg="Koneksi Ke Server Gagal !";
        }
        return status;
     }
   private boolean stat ;
    public void run() {
        if (proseskirim()){
            Sukses(HTTPResponse);            
            stat=true;
        }
        else{
            SetMSG(HTTPErrMsg);
            stat=false;
        }    
        gg.setValue(10);
    }
    
    public void SetMSG(String Msg){
        gg.setLabel("Selesai..!");
        setTitle("Proses Gagal !");
        delete(1);
        append("\n "+Msg);
        addCommand(bck);
    }
   
    public void commandAction(Command c, Displayable d) {
       dis.setCurrent(bckscreen);
    }    
    
    public abstract void Sukses(String ResponseMsg);
    public String getResponse(){
        if(stat){
            return HTTPResponse;
        }
        else{
            return HTTPErrMsg;
        }
    }
}

Categories: J2ME, Java, RMS Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: