One, network operation
In the network operation Java has provided me with a set of APIs for network operation, in Android development we can still do this set of APIs to do development. Here's a simple example of how the network operates under Android.
Click the download button in the image to load a picture from the server
Second, XML layout file
The layout file uses a linear layout and uses only one button and one image
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Xidian.dy.com.chujia.MainActivity"> <ButtonAndroid:id= "@+id/btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/delete" /> <ImageViewAndroid:id= "@+id/img"Android:layout_width= "100DP"Android:layout_height= "100DP"android:src= "@drawable/dog1" /></LinearLayout>
Third, Mainactivity.java
In code, when a click event occurs, a HttpURLConnection object is created by using a URL to make a connection to the server, initiating an HTTP request. When the request succeeds, it indicates that the data is already in memory. At this point, we should get the data from memory and encapsulate it as a bitmap object, which is provided by Android and is designed to display directly from the download images on the Internet. These operations are all placed in sub-threads, one reason is that Google requires this, on the other hand
PackageXidian.dy.com.chujia;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classMainactivityextendsappcompatactivity {Private StaticImageView img; Staticmainactivity Ma; StaticHandler Handler; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button btn=(Button) Findviewbyid (R.ID.BTN); Ma= This; Handler=NewHandler () {@Override//This method refreshes the UI in the main thread Public voidhandlemessage (Message msg) {if(img! =NULL){ if(Msg.what = = 0) Img.setimagebitmap ((Bitmap) msg.obj); ElseToast.maketext (MA,"Picture Download Failed", Toast.length_short). Show (); } } }; if(BTN! =NULL) Btn.setonclicklistener (NewMylister ()); } classMylisterImplementsview.onclicklistener{@Override Public voidOnClick (View v) {img=(ImageView) Findviewbyid (r.id.img); NewThread () {@Override Public voidrun () {Try{URL URL=NewURL ("Http://192.168.0.119/dog.jpg"); //Create a Connection objectHttpURLConnection conn =(HttpURLConnection) url.openconnection (); //Set Request ModeConn.setrequestmethod ("GET"); //Setting the TCP connection timeoutConn.setconnecttimeout (5000); //Setting HTTP response TimeoutsConn.setreadtimeout (5000); //Establish a connectionConn.connect (); Message msg=Handler.obtainmessage (); if(Conn.getresponsecode () = = 200) {InputStream is=Conn.getinputstream (); Bitmap BM=Bitmapfactory.decodestream (IS); Msg.what= 0; Msg.obj=BM; Handler.sendmessage (msg); } Else{msg.what= 1; Handler.sendmessage (msg); } toast.maketext (mainactivity. This, "Download Failed", Toast.length_short). Show (); } Catch(Exception e) {e.printstacktrace (); }}}.start (); } }}
Android Network operation (1)