Today learned to use the HTTP protocol, from the Android client to the Tomcat server to send the request to get, inadvertently found himself writing the Handler class was prompted by Android: This Handler class should is static or Leaks might occur. To Google on the type, found that many netizens have done an explanation, but also deeply felt, the compiler hints, sometimes really want to pay attention to, this to their own study also has a great help.
Although several lines of code, but in order to be practical, I would like to stick to the actual point of the example, Activity:
Package Spt.http.get.activity;import Java.lang.ref.weakreference;import Spt.http.get.assist.SendDataToServer; Import Android.app.activity;import android.os.bundle;import Android.os.handler;import Android.os.Message;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import Android.widget.Toast;public Class Mainactivity extends Activity {//view.private EditText edt_name = null;private EditText edt_pwd = null;private Butto n BTN_OK = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); Initlistener ();} /** * Initialize view. */private void Initview () {edt_name = (EditText) Findviewbyid (r.id.edt_name); edt_pwd = (EditText) Findviewbyid (R.id.edt _PWD) Btn_ok = (Button) Findviewbyid (R.ID.BTN_OK);//test:edt_name.settext ("Hello"); Edt_pwd.settext ("abc");} /** uses static internal classes to resolve the ' This Handler class should is static or leaks might occur ' to avoid memory leaks. * @author Administrator * */private Static classStatushandler extends Handler {weakreference<mainactivity> imainactivity = Null;public Statushandler ( Mainactivity mainactivity) {imainactivity = new weakreference<mainactivity> (mainactivity);} @Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case senddatatoserver.send_success:// There are Imainactivity.get () and Imainactivity.getclass (). Toast.maketext (Imainactivity.get (), "Send Success", Toast.length_short). Show (); Break;case Senddatatoserver.send_fail: Toast.maketext (Imainactivity.get (), "Send Failed", Toast.length_short). Show (); Break;default:throw New RuntimeException (" Unknown send result! ");}} /** * Handler that handles the status of whether the send is successful. */private final Handler Handler = new Statushandler (this);/** * Initialize listener. */private void Initlistener () {Btn_ok.setonclicklistener (new Button.onclicklistener () {@Overridepublic void OnClick ( View v) {String name = Edt_name.gettext (). toString (); String pwd = Edt_pwd.gettext (). toString (); if (Name.isempty () | | | Pwd.isempty ()) {Toast.maketext (Mainactivity.this, " User name and password cannot be empty ", toast.length_short). Show (); return;} New Senddatatoserver (handler). Send (name, PWD);}});}}
Handler is declared as static to resolve a possible ' memory leak ' because, in general, the message object will hold an implicit reference to the external class when it is placed into the messages queue by handler, and the There is no implicit reference to the outer class. For example, the Message object references a resource such as a view of the main thread (UI), and if it is not processed in time, the object he refers to will be occupied and the referenced object may be used for a long time, so that memory will be exhausted soon.
Ref:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html
Android-this Handler class should be static or leaks might occur.