The Android platform has three network interfaces available: java.net. * (standard java Interface), org. apache (Apache Interface), and android.net. * (Android Network Interface ). This article uses java.net. * (standard java Interface) to obtain a network image and display it in the ImageView control.
Java.net. * (standard java Interface) provides network-related classes, including stream and packet sockets, Internet protocol, and common Http processing (for example, create a URL and URLConnection/HttpURLConnection object, set connection parameters, connect to the server, write data to the server, and read data from the server ).
The following describes how to use the HttpURLConnection interface.
1. HttpURLConnection
1.1common methods of httpurlconnection
In Android Application Development, HttpURLConnection has the following common methods:
Abstract void discontent (); // close this link
String getContentEncoding (); // get the encoding of the transport response body
InputStream getErrorStream (); // gets the error stream returned from the server
String getRequestMethod (); // obtain the Request Method of the remote HTTP Server
Int getResponseCode (); // gets the response value of the remote HTTP server.
String getResponseMessage (); // gets the Response Message of the remote HTTP server.
Void setRequestMethod (String mothod); // sets the Request Method for the remote HTTP server.
1.2 common server response values
When we connect to a server, we need to use the getResponseCode () method to obtain the server response value and determine whether the network connection is normal. Common server response values are as follows:
HTTP_BAD_GATEWAY // 502 Gateway error
HTTP_BAD_REQUEST // 400 request Error
HTTP_CLIENT_TIMEOUT // 408 client timeout
HTTP_NOT_FOUND // 404 the server is not found
HTTP_ OK // 200 normal connection
HTTP_UNAVAILABLE // 503 network unavailable
1.3 instance
To display a network image in the ImageView control, you must first obtain the image resource from the network. The following code implements this function, and the retrieved image resources are returned as streams.
1 /**
2 * get an image from the network and return it as a stream
3 * @ return
4 */
5 public static InputStream getImageViewInputStream () throws IOException {
6 InputStream inputStream = null;
7 URL url = new URL (URL_PATH); // server address
8 if (url! = Null ){
9 // open the connection
10 HttpURLConnection httpURLConnection = (HttpURLConnection) url. openConnection ();
11 httpURLConnection. setConnectTimeout (3000); // set the network connection timeout time to 3 seconds.
12 httpURLConnection. setRequestMethod ("GET"); // set the request method to GET
13 httpURLConnection. setDoInput (true); // open the input stream
14 int responseCode = httpURLConnection. getResponseCode (); // get the server response value
15 if (responseCode = HttpURLConnection. HTTP_ OK) {// normal connection
16 inputStream = httpURLConnection. getInputStream (); // get the input stream
17}
18}
19 return inputStream;
20}
For the above Code, there are the following points to note:
(1) due to the network connection, some exceptions may occur, so throws IOException is used in the code to throw an exception. When calling this function, you can use try-catch to catch exceptions and handle exceptions accordingly.
(2) Statement URL url = new URL (URL_PATH); specifies the server path, that is, the address of the network image to be accessed. So how can I publish an image to the server? Here, we can build the server by using MyEclipse and Tomcat (for the construction method, refer to Android study note 20: Http protocol and Java Web programming. After the server is built, you only need to put the image in the WebRoot directory of the project, and then start the project, we can access the image on the server through the URL_PATH.
(3) Because network resources are used in this project, we also need. open the network access permission in xml, the implementation method is very simple, in AndroidManifest. add the following code to the xml file.
1 <uses-permission
2 android: name = "android. permission. INTERNET">
3 </uses-permission>
So far, we have obtained an image resource from the server in the form of a stream. How can we use this image resource stream to create a Bitmap image? Here, we can use the Static Bitmap decodeStream (InputStream inputStream) method of the BitmapFactory object to create a Bitmap image based on the input stream, and then use the setImageBitmap (Bitmap bitmap) of the ImageView) method to add the Bitmap image to the ImageView Control for display. The specific implementation code is as follows:
1 try {
2 InputStream inputStream = HttpUtils. getImageViewInputStream ();
3 Bitmap bitmap = BitmapFactory. decodeStream (inputStream );
4 mImageView. setImageBitmap (bitmap );
5} catch (IOException e ){
6
7}
1.4 instance Effects
In this tutorial, a picture cat.jpg (1024*768) is added to the webrootdirectory of the myhttpproject. As a network resource image, you can see the effect 1 of the resource image in the browser after running the project.
Figure 1 Resource image display in the browser
Obtain the image resource from the server and display it in effect 2 of the ImageView control.
Figure 2 network image display in ImageView