230 likes | 656 Views
Android networking. 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
E N D
Android networking Android networking
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 networking
Permission to do networking • If you Android application should use networking it must be given the right permission • Default: No networking • AndroidManifest.xml • <uses-permission android:name="android.permission.INTERNET" /> • Moderns emulator throws UnknownHostException • Solution: DNS handling in the emulator • http://stackoverflow.com/questions/2206822/no-internet-on-android-emulator-why-and-how-to-fix Android networking
Socket • Plain old sockets works on Android devices! • WebServices and UrlConnection uses sockets. • Example • EchoServer (NetBeans project) • Lee4socketNoTask + Lee4socket (Eclipse project) • In modern Android you should not used sockets from the UI thread, since it might make the GUI freeze. • Solution: More AsyncTask Android networking
AsyncTask,Just a few words • minSdkVersion > 9 • Focus on anti-freezing 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 networking
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. • Another special IP address 10.0.2.2 can be used to access the computer running the emulator. • Different in Genymotion Android networking
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/ • Example • Lee4Networking (Eclipse project) Android networking
Web servicesUsing 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 networking
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 networking
Web servicesusing 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 networking