Handler mechanism is mainly used in Android development for the main thread and sub-threading communication, the sub-thread sends the necessary information to the main thread, and then updates the UI in the main thread;
Package Com.example.webview;import Java.io.bufferedreader;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.net.httpurlconnection;import Java.net.url;import Android.os.Bundle;import Android.os.handler;import Android.os.message;import Android.support.v7.app.actionbaractivity;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Actionbaractivity implements onclicklistener{public static final int show_response=0;private TextView Reponsetext;private Button sendbutton;private Handler handler=new Handler () {/* (non-javadoc) * @see android.os.handler# Handlemessage (android.os.Message) */@Overridepublic void Handlemessage (Message msg) {//Receive message and go to update control contents on UI thread switch ( Msg.what) {case show_response:string response= (String) Msg.obj;reponsetext.settext (RESPONSE); Break;default: Toast.maketext (MainActivity.this, "Show fail!!", Toast.length_short). Show (); break;}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); sendbutton= (Button) Findviewbyid (r.id.send_request_id); reponsetext= (TextView) Findviewbyid (r.id.text_id); Sendbutton.setonclicklistener (this);} /* (non-javadoc) * @see Android.view.view.onclicklistener#onclick (android.view.View) */@Overridepublic void OnClick ( View v) {sendrequestwithhttpurlconnection (); Toast.maketext (Mainactivity.this, "onclick!1", Toast.length_short). Show (); /** * Open thread to initiate a network request */private void Sendrequestwithhttpurlconnection () {New Thread (new Runnable () {@Overridepublic void run ( {httpurlconnection connection=null;try {URL url=new url ("http://www.baidu.com"); connection= (HttpURLConnection) Url.openconnection (); Connection.setrequestmethod ("GET");//Set Connection Timeout connection.setconnecttimeout (8000);// Set Read Timeout connection.setreadtimeout (8000);//Get input stream, read InputStream INPUTSTREAM=CONNECTION.GEtinputstream (); BufferedReader reader=new BufferedReader (New InputStreamReader (InputStream));//below this StringBuilder The function is equivalent to a stringstringbuilder response=new StringBuilder (); while (Reader.readline ()!=null) {//read a line, Add the read to response inside Response.append (Reader.readline ());} Call Dandler's Obtainmessage method to get a Message object message Message=handler.obtainmessage ();/* A Message object, public static Message obtain (Handler h, int what, int arg1, int arg2, Object obj), H: Target Handler object for processing message; What: encoding of message; arg1: additional integer data ; arg2: additional integer data; obj: the object type data for the attachment. Return value: Returns a Message object from the global object pool. */message.what=show_response;message.obj=response.tostring ();//This is a sub-thread, this is handler send information to the main thread handler.sendmessage ( message);} catch (Exception e) {//Todo:handle exception}finally{connection.disconnect ();}}). Start (); Toast.maketext (mainactivity.this, "thread Start", Toast.length_short). Show (); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
I small white, please the great God is wrong will rectify! Push me.
ANDROID Httpurlconnection,httpclient and the simplest handler mechanism