Download presentation
Presentation is loading. Please wait.
Published byDavid Anderson Modified over 9 years ago
1
Android networking 1
2
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
3
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 – http://stackoverflow.com/questions/2206822/no- internet-on-android-emulator-why-and-how-to-fix http://stackoverflow.com/questions/2206822/no- internet-on-android-emulator-why-and-how-to-fix Android networking3
4
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
5
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
6
Accessing localhost from the emulator Normally we access servers running on our own computer using the special loop back IP address 127.0.0.1 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 – 10.0.2.2 for ordinary emulators – 10.0.3.2 for Genymotion emulator Android networking6
7
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 http://www.easj.dk/something/http://www.easj.dk/something/ – Example HttpConnectionExample Android networking7
8
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
9
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
10
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.