PackageCom.frank.mobilesafe;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URI;ImportJava.net.URL;Importjavax.net.ssl.HttpsURLConnection;Importorg.apache.http.HttpClientConnection;Importorg.apache.http.client.methods.HttpUriRequest;ImportOrg.json.JSONObject;ImportCom.frank.mobilesafe.utils.FileHelper;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.content.pm.PackageInfo;ImportAndroid.content.pm.PackageManager;Importandroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.Window;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classSplashactivityextendsActivity {Private Static Final intShow_update_dialog = 0; protected Static Final intRequest_failure = 1; protected Static Final intRequest_error = 2; Private Static Final intShow_home = 3; PrivateHandler Handler =NewHandler () {@Override Public voidhandlemessage (Message msg) {handlerstartmessage (msg.what); } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //requestwindowfeature (window.feature_no_title); set UntitledSetcontentview (R.layout.activity_splash); TextView tv_version=(TextView) Findviewbyid (r.id.tv_version); Tv_version.settext (GetVersion ()); Checkupdate (); } protected voidHandlerstartmessage (intWhat ) { Switch(what) { CaseSHOW_UPDATE_DIALOG:Toast.makeText ( This, "Time to upgrade", 0). Show (); Break; CaseShow_home://Toast.maketext (This, "Show home Activity", 0). Show ();showmainactivity (); Break; CaseREQUEST_FAILURE:Toast.makeText ( This, "Request_failure", 0). Show (); Break; default: Break; } }/*** Show main interface*/ Private voidshowmainactivity () {//TODO auto-generated Method StubIntent Intent =NewIntent ( This, Mainactivity.class); StartActivity (Intent); Finish (); } /*** Check for upgrades*/ protected voidcheckupdate () {NewThread () { Public voidrun () {LongStartTime =System.currenttimemillis (); Message msg=Message.obtain (); Try{URL URL=NewURL ("http://192.168.1.104/1.html");//Network link AddressHttpURLConnection conn =(httpurlconnection) URL. OpenConnection ();//Create a Connection objectConn.setrequestmethod ("GET");//Set Request ModeConn.setreadtimeout (5000);//set the request time-out period if(+ = Conn.getresponsecode ())//execute when you are successful{InputStream InputStream=Conn.getinputstream (); Filehelper Helper=NewFilehelper (); String content=Helper. Streamtostring (InputStream); LOG.I (A, content); Jsonobject obj=Newjsonobject (content); String version= (String) obj.get ("version"); LOG.I ("Version", version); if(Version = =getversion ()) {Msg.what=Show_update_dialog; } Else{msg.what=Show_home; } } Else{log.i ("A", "Request failed!" "); Msg.what=request_failure; } } Catch(Exception e) {log.i ("A", "Shibai"); Msg.what=Request_error; E.printstacktrace (); } finally { LongEndTime =System.currenttimemillis (); LongSpantime = endtime-StartTime; if(spantime<2000) { Try{Thread.Sleep (2000-spantime); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} handler.sendmessage (msg); } }; }.start (); } /*** Get version information * *@return */ protectedstring GetVersion () {string Versionstr= ""; Packagemanager Packmanger=Getpackagemanager (); Try{PackageInfo Info= Packmanger.getpackageinfo (Getpackagename (), 0); Versionstr= Info.versionname;//Gets the version information}Catch(namenotfoundexception e) {//TODO auto-generated Catch blockVersionstr = ""; E.printstacktrace (); } returnVersionstr; }}
Summary of issues:
1, in parsing the JSON when the need for the situation has been encountered an exception, error exception information for Org.json.JSONException:Value? of type java.lang.String cannot is converted to Jsonobject, which was traced to the Notepad used when saving the JSON text, This means that the use of the save JSON should be saved using a text editor such as EditPlus, save the format should be UTF8 no BOM format, so that when parsing can not cause an exception.
2, in the network to obtain data, because it is configured on the local IIS site, but the request data at the time of the request is not, encountered the first reason is no request to set the request mode and request time, after the request for other sites such as Baidu no problem, but the configuration of their own request is not, After scratching, the reason for the check is that there is a less backslash behind the HTTP. It's a careless idea!
3, online range access to resources outside the thread, you need to use the Java event mechanism for processing. The main methods are as follows:
Private New Handler () { @Override publicvoid handlemessage (Message msg) { Handlerstartmessage (Msg.what); } };
protected void checkupdate () {new Thread () { publicvoid run () { long startTime = system.currenttimemillis (); = Message.obtain (); = show_update_dialog; handler.sendmessage (msg); } }; }. Start ();
Android Development Learning notes-Implementing a networked detection program version