使用SAE的Storage來為Android應用提供版本更新的檢查和下載功能

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

因為Android的市場比較分散,有時候上傳和審核都麻煩。為了讓使用者能第一時間獲得更新,接下來要實現版本檢查和下載功能。
先在Storage裡放入應用的APK、一個json的文檔或者xml檔案,因為我比較喜歡用json,所以接下來就用json文檔。寫json文檔的時候記得不要用記事本,要用Notepad++之類的文字編輯器來寫,然後儲存成UTF-8無BOM的格式。不然android4.0以下版本解析會有問題。更新資料的格式:
{
      "version": 10,
      "message": "1.更新資訊1\n2.更新資訊2\n3.更新資訊3\n4.更新資訊4",
      "dowload": "http://****.sinaapp.com/FileDownload?filename=newfile.apk"
}
讓Android端去讀取Storage的json文檔裡面記錄著,應用的最新版本號碼、本次更新的內容、。然後解析json拿到資料,再對比應用的目前的版本看是否需要更新。

Android端的代碼:

 1 class CheckedUpdate extends AsyncTask<String, Integer, String> { 2       protected String doInBackground(String... params) { 3         ByteArrayOutputStream baos = null; 4         InputStream ips = null; 5         String str = ""; 6         baos = new ByteArrayOutputStream(); 7         try { 8             // 建立一個byte數組作為緩衝區,等下把讀取到的資料儲存在這個數組 9             byte[] buffer = new byte[1024];10             int len = 0;11             URL url = new URL("http://****.sinaapp.com/Download?filename=version.json");12             HttpURLConnection huc = (HttpURLConnection) url.openConnection();13             // GET請求方式14             huc.setRequestMethod("GET");15             // 設定讀取逾時16             huc.setReadTimeout(10000);17             // 設定連線逾時18             huc.setConnectTimeout(3000);19             // 獲得輸入資料流20             ips = huc.getInputStream();21             // 拿到伺服器返回的響應碼22             int hand = huc.getResponseCode();23             if (hand == 200) {24               // 讀取資料25               while ((len = ips.read(buffer)) != -1) {26                 baos.write(buffer, 0, len);27               }28               str = new String(baos.toByteArray(), "UTF-8");29             } else {30               Log.w(TAG, "伺服器返回碼" + hand);31             }32         } catch (ProtocolException e) {33             e.printStackTrace();34         } catch (MalformedURLException e) {35             e.printStackTrace();36         } catch (IOException e) {37             e.printStackTrace();38         } finally {39             try {40               if (baos != null) {41                 baos.close();42               }43               if (ips != null) {44                 ips.close();45               }46             } catch (IOException e) {47               e.printStackTrace();48             }49         }50         return str;51       }52       53       protected void onPreExecute() {54         Toast.makeText(context, "正在連網檢查中", Toast.LENGTH_SHORT).show();55       }56       protected void onPostExecute(String result) {57         int version = 0;58         String message = "";59         String dowload = "";60         if (TextUtils.isEmpty(result))61             return;62         try {63             JSONObject jsonObject = new JSONObject(result);64             version = jsonObject.getInt("version");65             // 獲得應用的目前的版本66             PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);67             // 如果大於目前的版本則下載新應用的APK68             if (version > packageInfo.versionCode) {69               message = jsonObject.getString("message");70               dowload = jsonObject.getString("dowload");71               // 彈出對話方塊詢問是否更新72               setDialog(message, dowload);73             } else {74               Toast.makeText(context, "已經是最新版本了", Toast.LENGTH_LONG).show();75             }76         } catch (NameNotFoundException e) {77             e.printStackTrace();78         } catch (JSONException e) {79             e.printStackTrace();80         }81       }82     }

下面是通過服務端獲得Storage裡存放檔案的方法。
伺服器端的代碼:

 1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2       SaeStorage storage = new SaeStorage(); 3       String domain = "domainname"; 4       String fileName = request.getParameter("filename"); 5       boolean isFile = storage.fileExists("updatafile", fileName); 6       // 檔案是否存在 7       if(isFile){ 8         String url = storage.getUrl(domain, fileName); 9         response.sendRedirect(url);10       }else{11         response.getWriter().write("Not Found");12       }13     }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.