Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close()
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close() DIS openDataInputStream() DIS openInputStream() DOS openDataOutputStream() DOS openOutputStream()
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close() DIS openDataInputStream() DIS openInputStream() DOS openDataOutputStream() DOS openOutputStream() String getURL() String getHost() String getPort() long getExpiration() long getLastModified() int getResponseCode()
HttpConnection API API for processing the URL: http – protocol – server search?hl=en&q=java – file hl=en&q=java – query string
HttpConnection API API for processing the URL: String url = “ HttpConnection conn = (HttpConnection) Connector.open(url); hc.getURL() -- “ hc.getProtocol() -– “http” hc.getHost() –- “ hc.getPort() hc.getFile() –- “search?hl=en&q=java” hc.getQuery() – “hl=en&q=java”
HttpConnection API Response Codes ( ) Successful Request 2xx 200 OK (HTTP_OK)-- the request has succeeded Client Error 4xx 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 5xx 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
HttpConnection API Reading and image through Http connection String url = “ 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
XML EXtensible Markup Language Markup language similar to HTML Not a replacement for HTML Designed to represent structured data No predefined tags – you create your own Simplifies data sharing and data transport
XML Example Everyday Italian Giada De Laurentiis
XML Example Representation of Book ( ) Everyday Italian Giada De Laurentiis You decide on the tags,,,, Tags must be nested properly Tags are case sensitive
XML Example Tags, attributes, values Everyday Italian Giada De Laurentiis book – the tag category – attribute “COOKING” – value for attribute category (can have multiple attributes per tag)
XML Example Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling Learning XML Erik T. Ray (
XML Parsing SAX Parser (Simple Api for XML) ( ) SAX Parser is a “push parser” runs through entire document and triggers events on tokens Selected methods void parse(InputSource is, DefaultHandler dh) parses the input stream and notifies the handler when tokens are discovered to handle tokens must create our own Handler class that extends DefaultHandler
XML Parsing DefaultHandler – process tokens discovered by parser Selected Methods // implement to handle beginning and end of document void startDocument() void endDocument() // override to handle discovery and end tags void startElement(String uri, String localName, String qName, Attributes attributes) void endElement(String uri, String localName, String qName)
XML Parsing Attributes class – retrieve information about the tag’s attributes Can extract information based on attribute index or name Selected methods void getLength() – number of attributes String getQName(int index) – get the attribute’s name by index String getQValue(int index) – get attribute’s value by index String getQValue(String qName) – get attribute’s value by its name
XML Parsing Using the SAX Parser HttpConnection hc = (HttpConnection) Connector.open(url); DataInputStream is = conn.openDataInputStream(); DefaultHandler dh = new MyHandler(); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = spf.newSAXParser(); parser.parse(is, dh);
Parsing MIDlet
XML Parsing with KXML KXML – lightweight parser (can use if phone does not support JSR 172) Download and save in the lib/ folder
XML Parsing with KXML KXML – lightweight parser (can use if phone does not support JSR 172) Download and save in the lib/ folder Selected methods void getEventType() – any of START_DOCUMENT, START_TAG, END_TAG, END_DOCUMENT, TEXT, COMMENT String getName() – get the tag’s name String getText() – get the text value of a tag String getAttributeCount() – number of attributes for current tag String getAttributeName(int i) – get i-th attribute’s name String getAttributeValue(int i) – get i-th attribute’s value
XML Parsing with KXML KXML – lightweight parser (can use if phone does not support JSR 172) Download and save in the lib/ folder Selected methods void getEventType() – any of START_DOCUMENT, START_TAG, END_TAG, END_DOCUMENT, TEXT, COMMENT String getName() – get the tag’s name String getText() – get the text value of a tag String getAttributeCount() – number of attributes for current tag String getAttributeName(int i) – get i-th attribute’s name String getAttributeValue(int i) – get i-th attribute’s value
XML Parsing with KXML InputStream is = hc.openInputStream(); Reader reader = new InputStreamReader(is); KXmlParser parser = new KXmlParser(); parser.setInput(reader); parser.next() while(parser.getEventType() != KXmlParser.END_DOCUMENT) { if (parser.getEventType() == KXmlParser.START_TAG) { } else if (parser.getEventType() == KXmlParser.END_TAG) { } else if (parser.getEventType() == KXmlParser.TEXT) { } parser.next }