generic connection framework connection fileconnectionsocketconnectionhttpconnection inputconnection...
Post on 15-Jan-2016
223 views
TRANSCRIPT
![Page 1: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/1.jpg)
Generic Connection FrameworkConnection
FileConnection SocketConnection HTTPConnection
InputConnection OutputConnection
StreamConnection
![Page 2: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/2.jpg)
Generic Connection FrameworkConnection
FileConnection SocketConnection HTTPConnection
InputConnection OutputConnection
StreamConnection
void close()
![Page 3: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/3.jpg)
Generic Connection FrameworkConnection
FileConnection SocketConnection HTTPConnection
InputConnection OutputConnection
StreamConnection
void close()
DIS openDataInputStream()DIS openInputStream()
DOS openDataOutputStream()DOS openOutputStream()
![Page 4: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/4.jpg)
Generic Connection FrameworkConnection
FileConnection SocketConnection HTTPConnection
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()
![Page 5: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/5.jpg)
HttpConnection API 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
![Page 6: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/6.jpg)
HttpConnection API API for processing the URL:
String url = “http://www.google.com/search?hl=en&q=java”;
HttpConnection conn = (HttpConnection) Connector.open(url);
hc.getURL() -- “http://www.google.com/search?hl=en&q=java”;
hc.getProtocol() -– “http”
hc.getHost() –- “www.google.com”
hc.getPort() -- 80
hc.getFile() –- “search?hl=en&q=java”
hc.getQuery() – “hl=en&q=java”
![Page 7: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/7.jpg)
HttpConnection API Response Codes (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html )
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
![Page 8: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/8.jpg)
Telnet Demo
![Page 9: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/9.jpg)
HttpConnection API 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
![Page 10: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/10.jpg)
HTTP MIDlet
![Page 11: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/11.jpg)
XMLEXtensible 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
![Page 12: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/12.jpg)
XML Example
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
![Page 13: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/13.jpg)
XML Example
Representation of Book (http://www.w3schools.com/xml/xml_tree.asp)
<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
You decide on the tags <book>, <title>, <author>, <year>,<price>
Tags must be nested properly
Tags are case sensitive
![Page 14: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/14.jpg)
XML Example
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)
![Page 15: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/15.jpg)
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>
</book>
</bookstore>
(http://www.w3schools.com/xml/xml_tree.asp)
![Page 16: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/16.jpg)
XML Parsing
SAX Parser (Simple Api for XML) (http://www.saxproject.org/)
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
![Page 17: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/17.jpg)
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)
![Page 18: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/18.jpg)
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
![Page 19: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/19.jpg)
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);
![Page 20: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/20.jpg)
Parsing MIDlet
![Page 21: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/21.jpg)
XML Parsing with KXML
KXML – lightweight parser (can use if phone does not support JSR 172)
Download and save in the lib/ folder
http://kxml.sourceforge.net/
![Page 22: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/22.jpg)
XML Parsing with KXML KXML – lightweight parser (can use if phone does not support JSR 172)
Download and save in the lib/ folder
http://kxml.sourceforge.net/
Selected methods
void getEventType() – any of START_DOCUMENT, START_TAG, END_TAG, END_DOCUMENT, TEXT, COMMENT
String getName() – get the tag’s nameString getText() – get the text value of a tag
String getAttributeCount() – number of attributes for current tagString getAttributeName(int i) – get i-th attribute’s nameString getAttributeValue(int i) – get i-th attribute’s value
![Page 23: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/23.jpg)
XML Parsing with KXML KXML – lightweight parser (can use if phone does not support JSR 172)
Download and save in the lib/ folder
http://kxml.sourceforge.net/
Selected methods
void getEventType() – any of START_DOCUMENT, START_TAG, END_TAG, END_DOCUMENT, TEXT, COMMENT
String getName() – get the tag’s nameString getText() – get the text value of a tag
String getAttributeCount() – number of attributes for current tagString getAttributeName(int i) – get i-th attribute’s nameString getAttributeValue(int i) – get i-th attribute’s value
![Page 24: Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection](https://reader036.vdocuments.us/reader036/viewer/2022062409/56649d625503460f94a453cf/html5/thumbnails/24.jpg)
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}