Because the Android market is more fragmented, sometimes uploading and auditing are troublesome. To enable users to get updates the first time, the next step is to implement version checking and download capabilities.
Put the app's apk, a JSON document, or an XML file in the storage, because I prefer JSON, so I'll use the JSON document next. When writing a JSON document, remember not to use Notepad, write it with a text editor like notepad++, and save it in a UTF-8-free format. Otherwise android4.0 the following version resolution will be problematic. Update the format of the data:
{
"Version": 10,
"Message": "1. Update information 1\n2. Update information 2\n3. Update information 3\n4. Update Information 4",
"Dowload": "http://****.sinaapp.com/filedownload?filename=newfile.apk"
}
let the Android side to read the storage JSON document, the application of the latest version number, the content of this update. then parse the JSON to get the data, and then compare the current version of the app to see if it needs updating.
Android-Side code:
1 classCheckedupdateextendsAsynctask<string, Integer, string> {2 protectedstring Doinbackground (String ... params) {3Bytearrayoutputstream BAOs =NULL;4InputStream ips =NULL;5String str = "";6BAOs =NewBytearrayoutputstream ();7 Try {8 //create a byte array as a buffer, and wait for the read data to be stored in this array9 byte[] buffer =New byte[1024];Ten intLen = 0; OneURL url =NewURL ("Http://****.sinaapp.com/download?filename=version.json"); AHttpURLConnection Huc =(HttpURLConnection) url.openconnection (); - //GET request Mode -Huc.setrequestmethod ("GET"); the //Set Read Timeout -Huc.setreadtimeout (10000); - //Setting the connection timeout -Huc.setconnecttimeout (3000); + //Get input stream -IPS =Huc.getinputstream (); + //get the response code returned by the server A intHand =Huc.getresponsecode (); at if(Hand = = 200) { - //reading Data - while(len = ips.read (buffer))! =-1) { -Baos.write (buffer, 0, Len); - } -str =NewString (Baos.tobytearray (), "UTF-8"); in}Else { -LOG.W (TAG, "Server return code" +hand); to } +}Catch(protocolexception e) { - e.printstacktrace (); the}Catch(malformedurlexception e) { * e.printstacktrace (); $}Catch(IOException e) {Panax Notoginseng e.printstacktrace (); -}finally { the Try { + if(BAOs! =NULL) { A baos.close (); the } + if(IPs! =NULL) { - ips.close (); $ } $}Catch(IOException e) { - e.printstacktrace (); - } the } - returnstr;Wuyi } the - protected voidOnPreExecute () { WuToast.maketext (Context, "in-Network check", Toast.length_short). Show (); - } About protected voidOnPostExecute (String result) { $ intVersion = 0; -String message = ""; -String dowload = ""; - if(Textutils.isempty (result)) A return; + Try { theJsonobject Jsonobject =Newjsonobject (result); -Version = Jsonobject.getint ("Version"); $ //get the current version of the app thePackageInfo PackageInfo = Context.getpackagemanager (). Getpackageinfo (Context.getpackagename (), 0); the //Download the new app apk if it's larger than the current version the if(Version >packageinfo.versioncode) { theMessage = jsonobject.getstring ("message"); -Dowload = jsonobject.getstring ("Dowload"); in //Pop-up dialog asking whether to update the setdialog (message, dowload); the}Else { AboutToast.maketext (Context, "already the latest version.", Toast.length_long). Show (); the } the}Catch(namenotfoundexception e) { the e.printstacktrace (); +}Catch(jsonexception e) { - e.printstacktrace (); the }Bayi } the}
Here's how to get the files stored in the storage via the server.
Server-side code:
1 protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {2Saestorage storage =Newsaestorage ();3String domain = "DomainName";4String filename = request.getparameter ("filename");5 BooleanIsfile = storage.fileexists ("Updatafile", fileName);6 //whether the file exists7 if(isfile) {8String URL =storage.geturl (domain, fileName);9 response.sendredirect (URL);Ten}Else{ OneResponse.getwriter (). Write ("Not Found"); A } -}