Generic Connection Framework Connection Input Connection Output Connection

  • Slides: 24
Download presentation
Generic Connection Framework Connection Input. Connection Output. Connection Stream. Connection File. Connection Socket. Connection

Generic Connection Framework Connection Input. Connection Output. Connection Stream. Connection File. Connection Socket. Connection HTTPConnection

Generic Connection Framework Connection Input. Connection void close() Output. Connection Stream. Connection File. Connection

Generic Connection Framework Connection Input. Connection void close() Output. Connection Stream. Connection File. Connection Socket. Connection HTTPConnection

Generic Connection Framework Connection DIS open. Data. Input. Stream() DIS open. Input. Stream() Input.

Generic Connection Framework Connection DIS open. Data. Input. Stream() DIS open. Input. Stream() Input. Connection void close() DOS open. Data. Output. Stream() DOS open. Output. Stream() Output. Connection Stream. Connection File. Connection Socket. Connection HTTPConnection

Generic Connection Framework Connection DIS open. Data. Input. Stream() DIS open. Input. Stream() Input.

Generic Connection Framework Connection DIS open. Data. Input. Stream() DIS open. Input. Stream() Input. Connection void close() DOS open. Data. Output. Stream() DOS open. Output. Stream() Output. Connection Stream. Connection File. Connection Socket. Connection HTTPConnection String get. URL() String get. Host() String get. Port() long get. Expiration() long get. Last. Modified() int get. Response. Code()

Http. Connection API q API for processing the URL: http: //www. google. com/search? hl=en&q=java

Http. Connection API q API for processing the URL: http: //www. google. com/search? hl=en&q=java http – protocol www. google. com – server search? hl=en&q=java – file hl=en&q=java – query string

Http. Connection API q API for processing the URL: String url = “http: //www.

Http. Connection API q API for processing the URL: String url = “http: //www. google. com/search? hl=en&q=java”; Http. Connection conn = (Http. Connection) Connector. open(url); hc. get. URL() -“http: //www. google. com/search? hl=en&q=java”; hc. get. Protocol() -– “http” hc. get. Host() –- “www. google. com” hc. get. Port() -- 80 hc. get. File() –- “search? hl=en&q=java” hc. get. Query() – “hl=en&q=java”

Http. Connection API q Response Codes (http: //www. w 3. org/Protocols/rfc 2616 -sec 10.

Http. Connection API q Response Codes (http: //www. w 3. org/Protocols/rfc 2616 -sec 10. html ) Successful Request 2 xx • 200 OK (HTTP_OK)-- the request has succeeded Client Error 4 xx • 403 Forbidden (HTTP_FORBIDDEN) -- server is refusing to fulfill the request • 404 Not Found (HTTP_NOT_FOUND)-- server has not found a matching URI Server Error 5 xx • 500 Internal Server Error (HTTP_INTERNAL_ERROR) -- unexpected error on server • 503 Service Unavailable (HTTP_UNAVAILABLE) -- server currently unable to handle request due to temporary overloading or maintenance

Telnet Demo

Telnet Demo

Http. Connection API q Reading and image through Http connection String url = “http:

Http. Connection API q Reading and image through Http connection String url = “http: //www. cs. gettysburg. edu/~ilinkin/logo. gif”; 1. create a connection to the url 2. obtain a data input stream from the connection 3. check the length of the content 3. 1 if length > 0: read whole image at once else: read image in chunks until nothing read 4. close the connection

HTTP MIDlet

HTTP MIDlet

XML EXtensible Markup Language q Markup language similar to HTML q Not a replacement

XML EXtensible Markup Language q Markup language similar to HTML q Not a replacement for HTML q Designed to represent structured data q No predefined tags – you create your own q Simplifies data sharing and data transport

XML Example <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30. 00</price> </book>

XML Example <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30. 00</price> </book>

XML Example q Representation of Book (http: //www. w 3 schools. com/xml_tree. asp) <book>

XML Example q Representation of Book (http: //www. w 3 schools. com/xml_tree. asp) <book> <title>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30. 00</price> </book> q You decide on the tags <book>, q Tags must be nested properly q Tags are case sensitive <title>, <author>, <year>, <price>

XML Example q Tags, attributes, values <book category=“COOKING”> <title lang=“en”>Everyday Italian</title> <author>Giada De Laurentiis</author>

XML Example q Tags, attributes, values <book category=“COOKING”> <title lang=“en”>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30. 00</price> </book> <book category=“COOKING”> • book – the tag • category – attribute • “COOKING” – value for attribute category (can have multiple attributes per tag)

XML Example <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30. 00</price>

XML Example <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30. 00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29. 99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39. 95</price> </bookstore> (http: //www. w 3 schools. com/xml_tree. asp)

XML Parsing q SAX Parser (Simple Api for XML) (http: //www. saxproject. org/) q

XML Parsing q SAX Parser (Simple Api for XML) (http: //www. saxproject. org/) q SAX Parser is a “push parser” runs through entire document and triggers events on tokens q Selected methods void parse(Input. Source is, Default. Handler dh) • • parses the input stream and notifies the handler when tokens are discovered to handle tokens must create our own Handler class that extends Default. Handl

XML Parsing q Default. Handler – process tokens discovered by parser q Selected Methods

XML Parsing q Default. Handler – process tokens discovered by parser q Selected Methods // implement to handle beginning and end of document void start. Document() void end. Document() // override to handle discovery and end tags void start. Element(String uri, String local. Name, String q. Name, Attributes attributes) void end. Element(String uri, String local. Name, String q. Name)

XML Parsing q Attributes class – retrieve information about the tag’s attributes q Can

XML Parsing q Attributes class – retrieve information about the tag’s attributes q Can extract information based on attribute index or name q Selected methods void get. Length() – number of attributes String get. QName(int index) – get the attribute’s name by index String get. QValue(int index) – get attribute’s value by index String get. QValue(String q. Name) – get attribute’s value by its name

XML Parsing q Using the SAX Parser Http. Connection hc = (Http. Connection) Connector.

XML Parsing q Using the SAX Parser Http. Connection hc = (Http. Connection) Connector. open(url); Data. Input. Stream is = conn. open. Data. Input. Stream(); Default. Handler dh = new My. Handler(); SAXParser. Factory spf = SAXParser. Factory. new. Instance(); SAXParser parser = spf. new. SAXParser(); parser. parse(is, dh);

Parsing MIDlet

Parsing MIDlet

XML Parsing with KXML q KXML – lightweight parser (can use if phone does

XML Parsing with KXML q KXML – lightweight parser (can use if phone does not support JSR 172) q Download and save in the lib/ folder http: //kxml. sourceforge. net/

XML Parsing with KXML q KXML – lightweight parser (can use if phone does

XML Parsing with KXML q KXML – lightweight parser (can use if phone does not support JSR 172) q Download and save in the lib/ folder http: //kxml. sourceforge. net/ q Selected methods void get. Event. Type() – any of START_DOCUMENT, START_TAG, END_DOCUMENT, TEXT, COMMENT String get. Name() – get the tag’s name String get. Text() – get the text value of a tag String get. Attribute. Count() – number of attributes for current tag String get. Attribute. Name(int i) – get i-th attribute’s name String get. Attribute. Value(int i) – get i-th attribute’s value

XML Parsing with KXML q KXML – lightweight parser (can use if phone does

XML Parsing with KXML q KXML – lightweight parser (can use if phone does not support JSR 172) q Download and save in the lib/ folder http: //kxml. sourceforge. net/ q Selected methods void get. Event. Type() – any of START_DOCUMENT, START_TAG, END_DOCUMENT, TEXT, COMMENT String get. Name() – get the tag’s name String get. Text() – get the text value of a tag String get. Attribute. Count() – number of attributes for current tag String get. Attribute. Name(int i) – get i-th attribute’s name String get. Attribute. Value(int i) – get i-th attribute’s value

XML Parsing with KXML Input. Stream is = hc. open. Input. Stream(); Reader reader

XML Parsing with KXML Input. Stream is = hc. open. Input. Stream(); Reader reader = new Input. Stream. Reader(is); KXml. Parser parser = new KXml. Parser(); parser. set. Input(reader); parser. next() while(parser. get. Event. Type() != KXml. Parser. END_DOCUMENT) { if (parser. get. Event. Type() == KXml. Parser. START_TAG) { } else if (parser. get. Event. Type() == KXml. Parser. END_TAG) { } else if (parser. get. Event. Type() == KXml. Parser. TEXT) { } parser. next }