Android Networking - Tutorial
Topics covered
Android Networking - Tutorial
Topics covered
Android primarily uses the Apache HttpClient library for network operations, which is the preferred way due to its features and integration. The system also allows the use of the java.net package, but internally, it uses the Apache library. Additionally, AndroidHttpClient can be used from Android 2.2 onwards, enabling the specification of user agents and supporting SSL and GZIP compression. Access to network functionality requires the "android.permission.INTERNET" permission in the application's manifest file .
In Android, network resources are accessed using HttpClient for establishing connections and executing HTTP methods such as HttpGet. HttpResponse is used to manage responses from these requests. StreamReader and BufferedReader classes are utilized to read data from the connection, which is then appended and displayed to the user using a TextView within the application .
User preferences in Android are stored using the SharedPreferences system, where data can be committed using an editor object. Preferences are loaded using getSharedPreferences and set via the editor's putString method. When the application pauses, preferences are saved by committing changes, ensuring they are preserved for subsequent sessions .
The XmlPullParser class provides a lightweight and efficient way to parse XML files in Android. It is specifically designed for Android and offers an example in its Javadoc for ease of use. This class allows developers to parse XML data without the overhead of the traditionally heavier SAX and DOM parsers while offering similar functionality .
AndroidHttpClient, introduced with Android 2.2, builds upon DefaultHttpClient by adding features like allowing the designation of custom user agents, SSL support, and handling of GZIP compressed data, which improves performance and security for network communications. It aims for better resource management and performance optimization compared to DefaultHttpClient .
Handling lifecycle events, particularly onPause(), is crucial for managing network operations and user data, as it ensures that data is saved and state is preserved when an application goes into the background. onPause() is used to store the last URL input by the user into SharedPreferences, ensuring this information is retained and reloaded appropriately, which is essential for providing a seamless user experience across sessions .
The document outlines that developers should handle exceptions robustly when performing network operations. Specifically, try-catch blocks are used to manage potential issues during HTTP requests, such as connectivity problems or malformed URLs, by setting error messages in the TextView upon catching exceptions. Developers should anticipate and programmatically handle network variability to ensure stability in application performance .
For an Android application to access the internet, it requires the "android.permission.INTERNET" permission. Additionally, to modify system settings, such as configuring a proxy, the application needs the "android.permission.WRITE_SETTINGS" permission .
Proxies in Android can be configured using the Settings class where the HTTP_PROXY setting is set with the proxy and port details. However, one must be aware that DNS resolving might not work properly behind a proxy, which is a known issue. This requires the "android.permission.WRITE_SETTINGS" permission to make such changes in the application .
The document outlines a method to check network availability using the ConnectivityManager system service. By obtaining the active NetworkInfo object, you can determine network availability by verifying if the networkInfo object is not null and whether it is connected .