1 / 10

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

shyla
Download Presentation

Android networking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android networking Android networking

  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 networking

  3. 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

  4. 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

  5. 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

  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. • Another special IP address 10.0.2.2 can be used to access the computer running the emulator. • Different in Genymotion Android networking

  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/ • Example • Lee4Networking (Eclipse project) Android networking

  8. 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

  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 networking

  10. 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

More Related