Minggu, 11 November 2012

Membuat Aplikasi Browser Sederhana dengan bahasa java

Assalamualaikum........

kali ini saya ingin berbagi bagaimana cara membuat browser sederhana dengan pemograman java
kan enak kalo kita browsing dari handphone make aplikasi buatan sendiri ^_^

Buka NetBean
buat project, dengan midlet  browser
lanjut.. ke bagian coding




     /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;




    /**
     * @author Rudie
     */
    public class browser extends MIDlet implements CommandListener, Runnable  {
        Form alamat,koneksi,tampil;
        Display tampilan;
        TextField uRl;
        StringItem labelny;
        Command yes,keluar,kembali;
        public browser(){
            alamat = new Form ("Browser coba-coba");
            koneksi = new Form ("Loading coy,,");
            tampil = new Form ("Server Reply");
            uRl = new TextField("URL :","",256,TextField.ANY);
            labelny = new StringItem(null,"Sedang Menyambungkan \nBentar ya...");
            yes = new Command("OK",Command.OK,0);
            keluar = new Command("Keluar",Command.EXIT,0);
            kembali = new Command ("Kembali",Command.BACK,0);
          
            }
        public void startApp() throws MIDletStateChangeException{
            tampilan = Display.getDisplay(this);
            alamat.append(uRl);
            alamat.addCommand(yes);
            alamat.addCommand(keluar);
            alamat.setCommandListener(this);
            koneksi.append(labelny);
            koneksi.addCommand(kembali);
            koneksi.setCommandListener(this);
            tampil.addCommand(kembali);
            tampil.setCommandListener(this);
            tampilan.setCurrent(alamat);
          
          
        }
      

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
          
        }

        public void commandAction(Command cmd, Displayable d) {
            if (cmd == yes) {

          Thread t = new Thread(this);

          t.start();

          tampilan.setCurrent(koneksi);

        } else if (cmd == kembali) {

          tampilan.setCurrent(alamat);

        } else if (cmd == keluar) {

          try {
            destroyApp(true);
          } catch (Exception e) {

          }

          notifyDestroyed();

        }
        }

        public void run() {
            InputStream is = null;

        HttpConnection conn = null;

        try {

          String url = uRl.getString();

          if (!url.startsWith("http://") && !url.startsWith("https://")) {

            url = "http://" + url;

          }

          conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);

          if (conn.getResponseCode() == HttpConnection.HTTP_OK) {

            is = conn.openInputStream();

            final int MAX_LENGTH = 128;

            byte[] buf = new byte[MAX_LENGTH];

            int total = 0;

            while (total < MAX_LENGTH) {

                int count = is.read(buf, total, MAX_LENGTH - total);
              //int count = is.read(buf, total, MAX_LENGTH – total);

              if (count < 0) {

                break;

              }

              total += count;

            }

            is.close();

            String reply = new String(buf, 0, total);

            labelny.setText(reply);

          } else {

            labelny.setText("Gagal: error " + conn.getResponseCode() + "\n"+ conn.getResponseMessage());

          }

          conn.close();

          tampilan.setCurrent(tampil);

        } catch (IOException ex) {

          System.out.println(ex);

          ex.printStackTrace();

          Alert alert = new Alert("Error","Koneksi Gagal.", null,AlertType.ERROR);

          alert.setTimeout(Alert.FOREVER);

          tampilan.setCurrent(alert, alamat);

          return;

        }

      }
          
        }



silahkan di run
Selamat mencoba :)

Posting Lebih Baru Posting Lama Beranda

0 komentar:

Posting Komentar