Android networking 1
Network programming with Android If your Android is connected to a WIFI, you can connect to servers using the usual Java API, like – Sockets Low level, general – URLConnection Higher level – HttpURLConnection Higher level, targeted at the HTTP protocol Useful with web services – Etc. Android networking2
Permission to do networking If you Android application will use networking it must be given the right permission – Default: No networking AndroidManifest.xml – Some emulator throws UnknownHostException – Solution: DNS handling in the emulator – internet-on-android-emulator-why-and-how-to-fix internet-on-android-emulator-why-and-how-to-fix Android networking3
Socket Plain old sockets works on Android devices! – WebServices and UrlConnection uses sockets. Example – EchoServer (NetBeans project) – SocketNoTask + SocketUsingTask In modern Android you should not used sockets from the UI thread, since it might make the GUI iresponsive. Solution: More AsyncTask Android networking4
AsyncTask, Just a few words minSdkVersion > 9 – Focus on responsive GUIs – Slow operations cannot be performed from the UI thread Network operations are slow! – Slow operations must be moved to background threads. The class AsyncTask can help you! – Generic: Parameter type, progress type, result type – doInBackGround: The method you want to execute in a background thread – onPostExecute: Method called when doInBackGround has finished. Android networking5
Accessing localhost from the emulator Normally we access servers running on our own computer using the special loop back IP address However, in the emulator, this will refer to the emulator, not the computer running the emulator. A special IP addresses must be used to access the computer running the emulator – for ordinary emulators – for Genymotion emulator Android networking6
URLConnection and HttpURLConnection Two classes from the general Java API – Not special Android API URLConnection – General connection to a server. – Based on a URL HttpURLConnection – HTTP connection to a server – Based on an HTTP URL Like – Example HttpConnectionExample Android networking7
Web services Using SOAP Web services usually uses HTTP for transportation – HttpURLConnection can be used in the client (i.e. the Android application) Web services transport often transports either SOAP documents – SOAP is XML Example: Lee4WebServices2 (Eclipse project) Android networking8
Parsing XML document, Just a few words … The ordinary Java API has two packages related to parsing XML documents – SAX (Simple API for XML) Event based parser – DOM (Document Object Model) Builds an in-memory model of the document. The model can be traversed later Android networking9
Web services using JSON JSON (JavaScript Object Notation) – Pronounced JAY-SON (Like the name Jason) – JSON is not XML It is a simpler text format. Serializing structured data – Without using tags Described in RFC 4627 (2006) – JSON is simpler than SOAP does Uses less network band with Uses less memory + CPU time on the client computer – Example: WebServiceJSON (Eclipse project) Android networking10