One of the most basic features of the network connection, theAndroid System encapsulates the network connection, allowing developers to add more network functionality to the application faster. most Internet-connected android apps use http to send and receive data,andandroid includes two http client:httpurlconnection and Apache HttpClient. Developers can use either of these two clients to complete a network connection.
1, the basic steps of the network connection:
(1) Add Permissions: Manifest The following permissions must be included in the file
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
(2) Check network status and use Connectivitymanager of the Getactivenetworkinfo () and the isconnected () method to perform the check. as follows:
Connectivitymanager connmgr = (Connectivitymanager)
Getsystemservice (Context.connectivity_service);
Networkinfo networkinfo = Connmgr.getactivenetworkinfo ();
if (networkinfo! = null && networkinfo.isconnected ()) {
Get Data
} else {
Display Error
}
(3) performing network connections in a standalone thread: network operations involve unpredictable delays, and in order to prevent undesirable user experiences, the usual practice is to UI The network connection operation is performed in the independent outgoing thread.
(4) Connect and download the data to HttpURLConnection
URL url = new URL (myurl);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setreadtimeout (10000/* milliseconds */);
Conn.setconnecttimeout (15000/* milliseconds */);
Conn.setrequestmethod ("GET");
Conn.setdoinput (TRUE);
Conn.connect ();
int response = Conn.getresponsecode ();
is = Conn.getinputstream ();
(5) Stream objects converted to string objects
reader reader = null;
reader = new InputStreamReader (stream, "UTF-8");
char[] buffer = new Char[len];
Reader.read (buffer);
The contrast between 2,httpclient and HttpURLConnection.
httpclient and HttpURLConnection both support HTTPS, stream uploads and downloads, configurable timeouts,IPv6 , and connection pooling. Therefore, all two can complete basic network operation.
HttpClient is an open source project provided by Apache Open Source, which integrates it into Android. Have a lot of APIs, achieve more stable, fewerbugs . But at the same time, because the HttpClient API is too numerous to make it difficult for developers to upgrade and expand without breaking compatibility, the Android team is not actively working on upgrading and optimizing httpclient.
HttpURLConnection is a multi-purpose, lightweight HTTP client. His API is relatively simple, and developers are easier to use and extend it. However, the disadvantage is that there are some bugs in the previous version of Android2.3, such as: when calling the close () method on a readable inputstream , it is possible to invalidate the connection pool. The Android2.3 system fixes these issues and adds some improvements to HTTPS . In Android 4.0, some response caching mechanisms have been added, and the Google team will continue to optimize httpurlconnection .
It is generally recommended that systems prior to 2.3 use httpclient, while 2.3 and later versions use httpurlconnection.
"Android Official document Reading notes" Connection network