The following is Mainactiviy.java, which has the necessary annotations, which uses handler, as well as threads, workthread How to update the content of Mainthread before it can be updated.
Package Com.wyl.httptest2;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;public class Mainactivity extends Actionbaractivity { Button btn; Button btn2; EditText et; Handler MyHandler = new MyHandler (), @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); btn = (Button) Findviewbyid (R.id.btnid); btn2 = (Button) Findviewbyid (R.ID.BTNID2); Myonclicklistener L = new MyonclickliStener (); et = (EditText) Findviewbyid (R.id.edittextid); Btn.setonclicklistener (L);//Bind Listener Btn2.setonclicklistener (l );} Class Myonclicklistener implements onclicklistener{@Overridepublic void OnClick (View v) {if (V.getid () ==r.id.btnid) {// Principle: The main thread in the UI does not have direct access to the network System.out.println ("= = Thread Name:" +thread.currentthread (). GetName ()); Getinternetthread MyThread = New Getinternetthread (); Mythread.start ();} else if (V.getid () ==r.id.btnid2) {System.out.println ("You clicked the Clear button,");//If the button is cleared, then the contents of EditText Et.settext ("");}} /* Principle: Because Looper will automatically fetch data from the queue, and then automatically invoke the copy of the Handlemessage (Message msg) {} of the class that inherits the handler object. * The purpose of inheriting handler: * In order to write data obtained from Workthread to the UI (UI thread in Android, i.e. the main thread is not able to access the network) * @author Administrator * */class MyHandler Exte NDS handler{@Overridepublic void Handlemessage (Message msg) {//is printed out as the main thread, so we can simply remove the value of MSG, SYSTEM.OUT.PRINTLN (" = = What thread I am: "+thread.currentthread (). GetName ());//Remove MSG (remove Msg.obj), the Obj property of this binding is the value we really want string fhz = (string) Msg.obj;et.settext (FHZ);}} /* * Main thread (that is, front-click event-related threads) can not access the network directly, then theGetinternetthread this new thread (which is definitely workthread) and then accesses the network through this workthread, and this thread binds the network data to the obj attribute of the msg (just go to this step, UPDATE * The data in the foreground UI only needs to be handed to the public void Handlemessage (Message msg) * In the replication handler of the handler class, and this method is executed automatically. ) * @author Administrator * */class Getinternetthread extends thread{string line = "I am empty"; @Overridepublic void Run () {Syste M.OUT.PRINTLN ("Thread Name:" +thread.currentthread (). GetName ());//Gets the uristring uri = Et.gettext () in the EditText control. toString (); (!uri.contains ("/http")) {//If the URL does not contain htt://, then it will not be able to access the network message M = Myhandler.obtainmessage (); m.obj = "Fairy board, your URL is not http://!!! "; Myhandler.sendmessage (m); return;} SYSTEM.OUT.PRINTLN ("uri value:" +uri);//access network//Get client Object httpclient = new Defaulthttpclient ();//Get Request object and pass in Urihttpget get = new HttpGet (URI);//client Send Request object try {HttpResponse resp = Client.execute (get);//Get Status code, if it is 200 description successful int code = Resp.getstatusline (). Getstatuscode (); if (code = = 200) {//Get return value InputStream in = Resp.getentity (). getcontent (); BufferedReader reader = new BufferedReader (New InputStreamReader (in)), line = Reader.readline ();}} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Et.settext (line);//Set EditText value of message msg = Myhandler.obtainmessage (); msg.obj = line;// This step binds the content we need to write to the UI to the MSG myhandler.sendmessage (msg);}}
Android access to the network (normal use)