Android Online Upgrade related notes one (resolution server version compared to current version)

Source: Internet
Author: User

Approximate process: The Android client accesses an XML file on the server that encapsulates information such as the version number, compares the version on the server to the current version,

If it is lower than the server version, download the new version of the software on the server, install the replacement, and complete the upgrade.


First, use Tomcat to build a server for development testing.

Download Tomcat please refer to: http://blog.csdn.net/only_tan/article/details/25110625

1. Create your own project in Tomcat:

Create your own project folder under the \apache-tomcat-6.0.39\webapps directory, such as MyApp

Then put xml,apk files in the MyApp folder;

For example, I put a upgrade.xml and a test.apk file in my MyApp project;

2. Access to your project:

PC Browser opens Xml:http://localhost:8080/myapp/upgrade.xml

Android device turns on Xml:http://10.0.2.2:8080/myapp/upgrade.xml (or 10.0.2.2 For the cost machine IP)

Open the Apk file method Similarly, the normal situation will pop up the download dialog box.

For example, TEST.APK's http://10.0.2.2:8080/myapp/Test.apk

If the files in Tomcat are not downloadable (such as APK, etc.) access error, resolve the following:

Method 1:

Put the files you want to download into the Tomcat/webapps/root directory, access the file name (or http://10.0.2.2:8080/file name) downloaded by http://native ip:8080/;

Method 2: Add the following content in Apache-tomcat-6.0.39\conf\web.xml:

<mime-mapping> <extension>doc</extension><mime-type>application/msword</mime-type > </mime-mapping> <mime-mapping> <extension>xls</extension> <mime-type> application/msexcel</mime-type> </mime-mapping> <mime-mapping> <extension>pdf</ Extension> <mime-type>application/pdf</mime-type> </mime-mapping><mime-mapping> < Extension>zip</extension> <mime-type>application/zip</mime-type> </mime-mapping>< mime-mapping> <extension>rar</extension> <mime-type>application/rar</mime-type> </ Mime-mapping><mime-mapping> <extension>txt</extension> <mime-type>application/txt< /mime-type> </mime-mapping><mime-mapping> <extension>chm</extension> <mime-type> Application/mshelp</mime-type></mime-mapping>
Save, and then restart Tomcat.

Second, access the server, parse the contents of the XML file;

1. Contents of XML file on server: (there are mainly 3 elements, version number, URL address, related information)

<upgrade><version>2</version><url>http://10.0.2.2:8080/myapp/Test.apk</url>< About> version updated to 2.0, thank you! </about></upgrade>

2. Create a new entity class: (for XML content, encapsulate elements that need to be included)

public class Updatainfo {    private int version;      Private String URL;      Private String about;          public int getversion () {          return version;      }      public void setversion (int version) {          this.version = version;      }      Public String GetUrl () {          return URL;      }      public void SetUrl (String url) {          this.url = URL;      }      Public String Getabout () {          return on;      }      public void Setabout (String on) {          this.about = about;      }      }
3. Parse the XML content with the pull parser; (need to pass in a inputstream)
    Public Updatainfo Getupdatainfo (InputStream is) throws exception{Updatainfo info = null;            Xmlpullparser parser = Xml.newpullparser ();          Parser.setinput (IS, "GB2312");//Set parsed data source, encoded format int event = Parser.geteventtype ();  while (event! = xmlpullparser.end_document) {switch (event) {case xmlpullparser.start_document: Start parsing//can do initialization related work here info = new Updatainfo ();                      LOG.I ("Updatepullparser", "--start_document--");                 Break                 Case XMLPULLPARSER.START_TAG:LOG.I ("Updatepullparser", "--start_tag--");                String tag = Parser.getname ();                  if ("Version". Equals (Tag)) {Info.setversion (New Integer (Parser.nexttext ()));//Get version number }else if ("url". Equals (Tag)) {Info.seturl (Parser.nexttext ());//Get URL address}else if ("a Bout ". Equals (tag)) {info.Setabout (Parser.nexttext ());              Get related information} break;            Case xmlpullparser.end_tag://Reads an element, if there are multiple elements, into the container break;            Default:break;          } event = Parser.next ();   } return info; Returns a updatainfo entity}

4. Get the XML content and return a inputstream to parse the required;

  Public InputStream getXml () throws Exception {  String tag= "Urlconnect";         String Httpurl = "Http://10.0.2.2:8080/myapp/upgrade.xml";                HttpURLConnection conn = (httpurlconnection) new URL (Httpurl). OpenConnection ();        Conn.setreadtimeout (5*1000);  Sets the time of the connection timeout  //    Conn.setrequestmethod ("GET");        Conn.connect (); Start connecting        if (conn.getresponsecode () = =) {            InputStream is = Conn.getinputstream ();            return is;   return InputStream         } else {  log.e (TAG, "---connection failed---");        }        Conn.disconnect (); Disconnect        return null;    }
5. Call in a trigger event to get the server version number;

 private int mlocalversion = 1; Local version private int mserverversion = 2; Server version//call to obtain and parse XML methods (asynchronous or thread-in-action); private void Check () {new Thread () {@Override public void Ru   N () {//a method that requires a thread execution try {InputStream is = getXml ();  Get XML content Updatainfo info = getupdatainfo (IS);  Call parsing Method mserverversion = Info.getversion (); Get Server version log.i (TAG, "check--infoversion=" +info.getversion () + "infourl=" +info.geturl () + "infoabout=" +info.getabout                ());                } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();            }//Send a message to handler Mhandler.sendemptymessage (New Message (). what=1); }}.start ();} Handler message receive mechanism private Handler Mhandler =new Handler () {//handler received the corresponding message to refresh UI, etc. public void handlemessage (message        msg) {super.handlemessage (msg); if (msg.what = = 1) {//Receive message, herePerform UI-related actions, such as displaying the parsed content. }         }  };
6. Get the local version number, compared to the server version number;
Gets the local version method    void Getlocal () {        packageinfo packageinfo;        try {            packageinfo = Getapplicationcontext ()                    . Getpackagemanager (). Getpackageinfo (Getpackagename (), 0);            Mlocalversion = Packageinfo.versioncode;        } catch (Namenotfoundexception e) {            //TODO auto-generated catch block            e.printstacktrace ();        }    }
Here, the server version number and the local version number are already there, the rest is the comparison size;

if (Mlocalversion < mserverversion) {                      //have a new version, need to upgrade                  } else {                      //Do not need to upgrade                  }

------------------------------OK first record here, have time to continue-------------------thanks! -------------------




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.