T Modul 13 J2ME

52
J2ME GUI dan Interkoneksi Client Server Muhammad Zen S. Hadi, ST. MSc.

Transcript of T Modul 13 J2ME

Page 1: T Modul 13 J2ME

J2ME GUI danInterkoneksi Client Server

Muhammad Zen S. Hadi, ST. MSc.

Page 2: T Modul 13 J2ME

2

Contents

J2ME (Review)

GUI dalam J2ME (RadioButton dan CheckBox)

Passing parameter dgn radiobutton dan checkbox

Interkoneksi client server (PHP MySQL)

Page 3: T Modul 13 J2ME

3

Arsitektur Jaringan dgn J2ME

Page 4: T Modul 13 J2ME

4

Information Access and CommunicationBuying Movie Ticket

Page 5: T Modul 13 J2ME

5

Klas Utama dalam paket lcduiTo be discussed in this lecture

Page 6: T Modul 13 J2ME

6

Komponen ChoiceGroupMemberikan user pilihan dari list yang tersedia. Terdapat 3 format ChoiceGroup : EXCLUSIVE : satu pilihanMULTIPLE : banyak pilihanPOPUP : satu pilihan dengan tampilan popup

Page 7: T Modul 13 J2ME

7

Komponen ChoiceGroupFormat:– ChoiceGroup(String label, int choiceType)

Membuat obyek ChoiceGroup dan menentukantitle dan tipenya.

– ChoiceGroup(String label, int choiceType, String[] stringElements, Image[] imageElements)Membuat obyek ChoiceGroup, menentukan title dan tipe serta array dari string dan gambar untuknilai awal.

Untuk passing parameter dari pilihan user gunakangetSelectedIndex(), getString(int elementNum) andisSelected(int elementNum).

Page 8: T Modul 13 J2ME

8

Contoh TextFieldChoiceGroupTestForm Registrasi untuk memasukkan data pribadi danmenampilkannya di dalam console.

Page 9: T Modul 13 J2ME

9

Untuk Satu Pilihan (EXCLUSIVE)

Page 10: T Modul 13 J2ME

10

Untuk Satu Pilihan (POPUP)

Page 11: T Modul 13 J2ME

11

Passing Parameter dgn RadioButton

Page 12: T Modul 13 J2ME

12

Passing Parameter dgn RadioButton

Page 13: T Modul 13 J2ME

13

Banyak Pilihan (MULTIPLE)

Page 14: T Modul 13 J2ME

14

Passing Parameter dgn CheckBox

Page 15: T Modul 13 J2ME

15

Aplikasi untuk CheckBox

Untuk fungsi pauseApp(), destroyApp(), awal(), keluar() sama dengan program sebelumnya

Page 16: T Modul 13 J2ME

16

Page 17: T Modul 13 J2ME

17

Menampilkan banyak project

Page 18: T Modul 13 J2ME

18

INTERKONEKSI CLIENT SERVER

Page 19: T Modul 13 J2ME

19

IntroductionJ2ME provides networking features to the mobile devices. It is possible to get up-to-the-minute stock quotes or updated currency exchange rates on a mobile phone.The javax.microedition.io classes and interfaces handle the networking capability of MIDP.The java.io package provides input/output (I/O) capability to MIDP.

Page 20: T Modul 13 J2ME

20

J2ME Networking CategoriesJ2ME networking has 3 categories:– Low-level IP networking– HTTP networking– Secure networking

The most critical aspect of J2ME network connectivity is communication between a mobile device and Web server.

Page 21: T Modul 13 J2ME

21

Low-level IP networkingThis category involves socket, datagram, serial port, and file I/O communication. Socket-based communication conforms to the connection-oriented TCP/IP protocol. Datagram-based communication conforms to the connectionless UDP/IP protocol. e.g. URI for a datagram connection for sending to a server on a certain port:– datagram://123.456.789.12:1234

Low-level IP networking can also handle file I/O and can allow a MIDlet to use a local serial port.

Page 22: T Modul 13 J2ME

22

HTTP networkingThe communication between a mobile device and a Web server is based on HTTP (Hypertext Transfer Protocol). HTTP is a connection-oriented request-response protocol.

Page 23: T Modul 13 J2ME

23

The Connection FrameworkJ2ME networking was designed to address the diverse needs of a wide range of mobile devices. At the same time, the networking system must be device specific. To meet these challenges, it introduces the concept of a generic connection framework.Generic connection framework is to define the general aspects of the networking and file I/O in the form of Java interfaces for a broad range of handheld devicesLeave the actual implementations of these interfaces to individual device manufacturers.

Page 24: T Modul 13 J2ME

24

The Connection Framework

Page 25: T Modul 13 J2ME

25

URL Handling in J2MEURL handling in J2ME involves opening a connection to the Web server from the mobile device and handling data I/O between the two. The process happens in the following stages:– Setup– Connected– Closed

J2ME defines the javax.microedition.io.Connectorclass to create all the connection objects. In URL handling, Connector.open() is used to open a URL; it returns an HttpConnection object.

Page 26: T Modul 13 J2ME

26

URL Handling in J2METhe string parameter to the Connector.open() method is a valid URL. The URL string varies depending on the communication protocol, as Examples 1 through 5 below demonstrate.

Example 1. Invoking HTTP-based communicationConnection conn =

Connector.open("http://www.yahoo.com");

Example 2. Invoking stream-based socket communicationConnection conn =

Connector.open("socket://localhost:9000");

Page 27: T Modul 13 J2ME

27

URL Handling in J2MEExample 3. Invoking datagram-based socket communicationConnection conn =

Connector.open("datagram://:9000");

Example 4. Invoking serial port communicationConnection conn =

Connector.open("comm:0;baudrate=9000");

Example 5. Invoking file I/O communicationConnection conn =

Connector.open("file://myfile.dat");

Page 28: T Modul 13 J2ME

28

URL Handling in J2METhe Connector.open() method also accepts the access mode (values READ, WRITE, and READ_WRITE), and a flag to indicate that the caller wants a timeout notification. – static Connection open(String name, int mode)

– static Connection open(String name, int mode, boolean timeouts)

The method openInputStream() of Connector opens an input stream of bytes (java.io.InputStream).

Page 29: T Modul 13 J2ME

29

URL Handling in J2MESimilarly, a java.io.OutputStream representing an output stream of bytes.The counterparts of InputStream and OutputStreamare java.io.DataInputStream and java.io.DataOutputStream, respectively. A DataInputStream/DataOutputStream lets an application read/write primitive Java data types.

Page 30: T Modul 13 J2ME

30

Akses file text dari Server

server

Simpan file text dihttp://127.0.0.1/modu13/pesan1.txt

Page 31: T Modul 13 J2ME

31

Menghilangkan tampilan AirTime

Page 32: T Modul 13 J2ME

32

Aplikasi akses file text dari Server

Page 33: T Modul 13 J2ME

33

Aplikasi akses file text dari Server

Page 34: T Modul 13 J2ME

34

Akses file php dari Server

server

Simpan file php dihttp://127.0.0.1/modu13/lat1.php

Page 35: T Modul 13 J2ME

35

Aplikasi akses file php dari ServerSemua bagian sama seperti program sebelumnya

Page 36: T Modul 13 J2ME

36

Passing Parameters to ServerTypically, an HTTP request to a server is accompanied by information needed by the server to process the request.

Page 37: T Modul 13 J2ME

37

Passing Parameters to Servere.g. Login in case. A server must authenticate a client using the client’s user ID and password. The client sends the user ID and password along with the HTTP request. Two techniques are used to send data to the server:GET or POST request methods.The GET request method requires that data be concatenated to the URL of the server. The POST request method requires that each pair value be written to the output stream.

Page 38: T Modul 13 J2ME

38

Passing Parameters to ServerData sent to a server must be in a pair value set:

– field name– the value associated with the field.

The field name and value must be separated by an equal sign (=).The pair value sets are separated from the URL by a question mark (?). Each pair value set is separated from other pair value sets by an ampersand (&).The space character is converted to a plus (+) sign.For example, the following request sends two parameters: pOne="one bit" and pTwo="two"

http://127.0.0.1/modul13/simple.php?p=one+bit&pTwo=two

Page 39: T Modul 13 J2ME

39

Akses file php dgn parameter

server

Simpan file php dihttp://127.0.0.1/modu13/lat2.php

Page 40: T Modul 13 J2ME

40

Aplikasi Akses File php dgn parameter

Page 41: T Modul 13 J2ME

41

Akses file php dgn parameter (2)

server

Simpan file php dihttp://127.0.0.1/modu13/lat3.php

Page 42: T Modul 13 J2ME

42

Aplikasi Akses File php dgn parameter (2)

Page 43: T Modul 13 J2ME

43

Interaksi dengan Database (Review)Dalam pembuatan aplikasi berbasis Database –WAP, beberapa perintah di Database baik DML danDDL bisa digunakan dikarenakan aksesnya masihberbasis PHP.Beberapa tahapan dalam interkoneksi denganDatabase :Membuka koneksi ke server MySQL

$conn = mysql_connect (”host”,”username”,”password”);

Memilih databasemysql_select_db(”namadatabase”,$conn);

Memilih tabel dengan query$hasil = mysql_query(”SQL Statement”);

Mengambil record dari tabel$row = mysql_fetch_row($hasil);

Page 44: T Modul 13 J2ME

44

Contoh aplikasi berbasis J2ME - Database

Buat database KAMPUS, dengan tabel Mahasiswa yang memiliki field sebagai berikut, dengan NRP sebagaiPrimary Key :

Akan dibuat aplikasi untuk memasukkan data baru, danmenampilkan data dengan J2ME.

Page 45: T Modul 13 J2ME

45

Input Data ke Database

server

Simpan file php dihttp://127.0.0.1/modu13/prosesInsert.php

Page 46: T Modul 13 J2ME

46

Aplikasi Input Data ke Database

Page 47: T Modul 13 J2ME

47

Aplikasi Input Data ke Database

Page 48: T Modul 13 J2ME

48

Tampil Data dari Database

server

Simpan file php dihttp://127.0.0.1/modu13/prosesTampil.php

Page 49: T Modul 13 J2ME

49

Aplikasi Tampil Data dari Database

Page 50: T Modul 13 J2ME

50

Aplikasi Tampil Data dari Database

Page 51: T Modul 13 J2ME

51

TUGASBuat aplikasi untuk menghitung biaya total pemakaian PDAM :a. Buat databasenya

Golongan Biaya (m3)1 5002 10003 1500

b. Input user :Nama user : Fakhri [TextField]Golongan : 1 [ChoiceGroup dgn 1 pilihan]Pemakaian : 20 [TextField]

c. Output :Nama : FakhriTotal yang harus dibayar : Rp. 10000

Page 52: T Modul 13 J2ME

52

TUGASPresentasi tiap kelompok untuk proyeknya :a. Blok Diagram Sistemb. Sistem Database