First, the core steps of network programming
(1) Add Internet permissions in the manifest Androidmanifest.xml file.
(2) Code to connect to the network address
1th step: Create a URL
String Path = "Http://192.168.17.98:8080/img/news.xml"; URL url = new URL (path);
2nd step: Get the connection via URL HttpURLConnection
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
3rd Step: Set parameters for some requests via httpurlconnection connection
Conn.setrequestmethod ("GET"); The default request method is get, uppercase. Conn.setconnecttimeout (5); Link Network time-outs, seconds units.
The 4th step: Request response and get the server's response code, determine the status of the response code, the corresponding action.
int code = conn.getresponsecode (); /*200 representative get server resources full Department Success 206 request some resources */ if (code == 200) { //parse the input stream of the connection, Get the data and do other things. ... }
Second, the message mechanism in Android
After Android4.0, Google engineers are forcing time-consuming operations in Android, such as the above network access, copy
Large data) to run in a sub-thread, or the program will report the following error at run time:
Android.os.NetworkOnMainThreadException Accessing the network in the main thread
However, in a child thread there is often an action to update the UI (such as changing the text value of a component in activity), but updating the UI
Operation can only be done in the main thread, and if there is an action to update the UI in the child thread, the program will report the following error :
Android.view.viewrootimpl$calledfromwrongthreadexception:only the original
Thread that created a view hierarchy can touch it views. Only the main thread can update the UI
This creates a contradiction, so how to solve this contradiction? WORKAROUND: There are 2 ways:
The 1th type: message mechanism handler
1) Create a handler object on the member position of the class, and make a copy of its handlemessage (Message msg) method.
In this method, get the message that the child line path handed over, and then update the UI.
Create assistant Handler private Handler Handler = new Handler () {public void Handlemessage (Amessage msg) {//Get the type of message Switch (msg.what) {case loadsucess://represents getting data successfully//takes data out of String content = (string) msg.obj; To strongly turn//display the content of the source code tv_content.settext (contents); Break;case Loaderror://Represents failed to get data Toast.maketext (Getapplicationcontext () , "The resource accessed does not exist", 1). Show (); Break;case LOADEXCEPTION:Toast.makeText (Getapplicationcontext (), "Server Busy!!!", 1). Show (); break ;} }; };
2) Call the SendMessage (Message msg) method of the Handler object in the child thread to encapsulate the obtained data into
Message.
Create a Message object message msg = Message.obtain (); This way you get the message object, which reduces the number of times a message is created. You can also new object msg.what = loadsucess; The type of the tag message, int type. Msg.obj =content; This property can carry any data type//send a message to tell the system that I want to update the UI Handlemessage method will immediately execute Handler.sendmessage (msg);
This article is from the "line of the World" blog, please be sure to keep this source http://4259297.blog.51cto.com/4249297/1675916
Android day04-Network Programming