Public interface callback {public void onsucc (); Public void onfail (string MSG );}
////////////////////////
@ Override public void onclick (view v ){
This. getserverappversion (New callback (){
@ Override public void onsucc (){
New alertdialog. Builder (moreversionactivity. This). settitle ("version update detection"). setmessage ("the new version is detected. Do you want to download the update? "). Setpositivebutton ("yes", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {moreversionactivity. this. downloadapp ();}}). setnegativebutton ("no", null ). setcancelable (false ). show ();
}
@ Override public void onfail (string MSG) {// log. E ("findeventaddactivity", MSG );}
});
}
Public String getserverappversion (final callback ){
Requestparams Params = new requestparams ();
Communicationtools. communication (Const. get_server_app_version_path, Params, new jsonhttpresponsehandler () {@ override public void onsuccess (jsonobject data) {// whether data is uploaded successfully try {string serverversion = data. getstring ("versionno ");
String currentvsersion = moreversionactivity. This. getversion ();
If (serverversion! = NULL & serverversion. Contains ("S") {serverversion = serverversion. Replace ("S ","");}
If (! Serverversion. Equals (currentvsersion) {callback. onsucc ();} else {publicmethods. showtoasttopshort (getapplicationcontext (), "it is already the latest version! ") ;}} Catch (jsonexception e) {publicmethods. showtoasttopshort (getapplicationcontext ()," network connection failed! "); E. printstacktrace ();}
}
@ Override public void onfailure (throwable arg0) {publicmethods. showtoasttopshort (getapplicationcontext (), "network connection failed! "); Callback. onfail (" server connection failed ");}});
Return NULL ;}
//////////////////////////////////////// ///////////
New thread (New runnable () {@ override public void run () {logoff. prepare (); try {URL url = new URL (Const. get_server_app_download_path); httpurlconnection urlconn = (httpurlconnection) URL. openconnection (); urlconn. setrequestproperty ("Accept-encoding", "Identity"); urlconn. connect (); urlconn. setconnecttimeout (60*1000); inputstream is = urlconn. getinputstream (); message MSG = handler. obtainmescript E (0); MSG. OBJ = urlconn. getcontentlength (); MSG. sendtotarget (); downfile (is);} catch (exception e) {e. printstacktrace (); // publicmethods. showtoasttopshort (getapplicationcontext (), // "Download failed! "); New alertdialog. Builder (moreversionactivity. This). settitle (" prompt "). setmessage (" Download failed. Please check whether the network is normal and try again later! "). Setpositivebutton (" OK ", null). setcancelable (false). Show ();} Looper. Loop ();
}). Start ();
Messy code 1