Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

HttpConnection Teguh S.

Presentasi serupa


Presentasi berjudul: "HttpConnection Teguh S."— Transcript presentasi:

1 HttpConnection Teguh S

2 Koneksi Http J2ME memiliki kemampuan untuk berinteraksi dengan resource lain melalui media jaringan Koneksi jaringan dalam media internet yang paling terkenal adalah HTTP Secara umum protokol HTTP bersifat stateless seperti pada umumnya dalam aplikasi web-based Berbagai macam yang didukung meliputi media text, gambar(image), video, dll Interface yang paling umum digunakan untuk koneksi jaringan adalah HttpConnection

3 Interface HttpConnection
Untuk menciptakan instan koneksi http digunakan bentuk sintax berikut: HttpConnection http = (HttpConnection) Connector.open(url); Url terdiri dari protokol://alamat:port/parameter Contoh: String url = “

4 Mendefinisikan Header Request
Koneksi yg dibangun dapat ditentukan dengan metode: GET, POST, HEAD, PUT dll http.setRequestMethod(HttpConnection.GET); Sedangkan untuk menentukan header pada request HTTP digunakan bentuk metode setRequestProperty(nama-property, nilai), contoh: http.setRequestProperty(“Content-Length”, “100”); http.setRequestProperty(“If-Modified-Since”, “mon, 20 Apr :23:52 GMT”); http.setRequestProperty(“User-Agent”, “Profile/MIDP-2.0 Configuration/CLDC-1.1”); Request Header untuk metode POST, ditambahkan http.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);

5 Melewatkan data Request pada metode GET/POST
Untuk melewatkan data pada metode GET: pada metode GET, data lewatkan pada URL sebagai parameter, Contoh: Sedangkan untuk melewatkan data pada metode POST, dimanfaatkan obyek Class OutputStream, contoh: String data = “nim= &nama=bejo”; http.setRequestProperty(“Content-Length”, data.length()+“”); http.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”); OutputStream os = http.openOutputStream(); os.write(data.getBytes());

6 Mengelola Respon dari Server
Hasil respon dari server pada protokol HTTP digambarkan melalui status : 1xx—information 2xx—success 3xx—redirection 4xx—client errors 5xx—server errors Contoh : HTTP/ OK HTTP/ Bad Request HTTP/ Internal Server Error Untuk mengambil status dari server digunakan metode getResponseCode() atau getResponseMessage(), contoh: if (http.getResponsCode()==200){ //download hasil respon dr server }

7 Mengelola Respon dari Server(2)
Hasil respon dari server direpresentasikan dalam bentuk byte data hexadesimal Digunakan obyek kelas InputStream untuk menerima hasil respon http, adapun contoh pemanfaatannya sbb: InputStream is = http.openInputStream(); Selanjutnya digunakan obyek kelas ByteArrayOuputStream untuk mengambil satu-persatu byte-byte data. ByteArrayOutputStream baos = new ByteArrayOutputStream(); int k; while (( k = is.read() ) != -1) baos.write(k); Ubah data ke tipe string byte [] dt = baos.toByteArray(); String hasil = new String(dt);

8 Aplikasi Input Mahasiswa
Aplikasi terdiri : aplikasi server (*.php) dan aplikasi client (j2me) Aplikasi Server : Database MySQL : test tabel : mhs (nim,nama,prodi) Aplikasi client : org.sinus.lab.SiakadMidlet

9 Aplikasi Server (simpanmhs.php)

10 Aplikasi Client (Siakad)

11 Aplikasi Client (Siakad)

12 Aplikasi Input Mahasiswa (Request)

13 Aplikasi Input Mahasiswa (Response)

14 Aplikasi Input Mahasiswa (Data)


Download ppt "HttpConnection Teguh S."

Presentasi serupa


Iklan oleh Google