Android Web Connectivity
Web Connectivity
Web
Resource Formats
A global collection of useful resources
Client-server architecture connecting users (through agents) with webservers over the internet using standard protocols
Basic Protocol : HTTP
Web services support information exchange with other applications
Native mobile application acts as a client / agent and responsible for
interpreting / parsing the resource
HTML, XML, JSON, Image, etc
Java / Android API
URL
HttpURLConnection
HTTP Protocol
A simple message-based protocol
Message Types
Request
represents client request to access a resource (identified by URI)
Different request methods supported (e.g. GET, POST, etc)
Response
Represents server response capturing resource representation in one of the supported formats
Example request
Example response
GET [Link] HTTP/1.1
Host: [Link]
HTTP/1.1 200
Content-type: text/html
Content-length: xx
<html>
<head> </head>
<body> Google </body>
</html>
1. Open connection
with web resource
try{
URL url = new URL("../resource");
HttpURLConnection connection = (HttpURLConnection) [Link]();
[Link](10000);
[Link](15000);
[Link]("GET");
[Link](true);
[Link]();
StringBuilder content = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader( [Link]() ) );
while( (line = [Link]()) != null ){
[Link](line);
}
line = [Link]();
parse(line);
} catch(Exception ex) {
[Link]();
}
3. Parse
2. Download resource
as a stream
Example: XML resource
<pictures>
<category name="Architecture" count="3">
<im title=... url=... description=... />
<im title=... url=... description=... />
<im title=... url=... description=... />
</category>
<category name="Art" count="3">
<im title=... url=... description=... />
<im title=... url=... description=... />
<im title=... url=... description=... />
</category>
</pictures>
XML Parsing
DOM
Serial
Tree-based
Parses entire XML document and provides a tree-based representation of elements
in form of a DOM
Easy-to-use
Memory intensive as well as slow
Event-based
Parses entire XML document and generates events when a specific XML construct
encountered
Requires customized code for handling events
Efficient with low memory footprint
Pull
A good compromise between DOM and Serial parsing
Underlying implementation based upon Serial parsing
XML DOM
XML Pull Parsing
Simple Event Model
START_DOCUMENT
START_TAG
TEXT
END_TAG
END_DOCUMENT
Recursive descent parsing
Instead of parsing and managing entire document
Parse only the portion interested in and skip the rest
pictures = new ArrayList<Picture>();
try{
XmlPullParser parser = [Link]();
[Link](new StringReader(xml));
int event = [Link]();
while(event != XmlPullParser.END_DOCUMENT){
if(event == XmlPullParser.START_TAG &&
[Link]().equals("category") ) {
category = [Link](null,"name");
}
if(event == XmlPullParser.START_TAG &&
[Link]().equals("im") ){
String url = [Link](null,"url");
String description = [Link](null,"description");
[Link](new Picture(url,description,category));
}
event = [Link]();
}
} catch(Exception ex){ }
Best practices
Download resources in a separate thread, AsyncTask
Check for connectivity changes
Adapt according to network state
Use of broadcast receivers and connectivity manager
Cache data
Wi-fi
Mobile radio
No connection
Cache size
Synchronization frequency
Strategies
Preferences
Allow user to specify connectivity and cache preferences