Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 基於 IIS 的 APK 下載(三):用 JSON 傳輸更新資料

編輯:關於 Android 程式設計

在〈Android 基於 IIS 的 APK 下載(二)顯示更新列表〉一文中,我們已在介面呈現了更新列表。那麼這份更新資料從何而來?關鍵在於 UpdateItemsAdapter 構造函數中傳入的 updateItems 參數,所以現在的重點就是取得它。

由於資料是透過網路傳輸,常見的格式有 JSON 與 XML。相較之下 JSON 更輕量,且 Java 有現成庫可解析,因此選擇用 JSON 實作。要從網路取資料必須有對應的 URL 與伺服器,這裡以 IIS 作為伺服器(後續文章會說明),並假設 IIS 已架設完成且在 MIME 中加入了對 json 檔案的支援。

UpdateLoader.java

package com.example.apkupdate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.kitsp.httpsp.RequestSp;

public class UpdateLoader {
    private String _updateUrl = "https://192.168.1.5:9000/update.json";

    @SuppressWarnings("finally")
    public List<UpdateItem> AnalyUpdate() {
        List<UpdateItem> updateItems = null;
        try {
            HttpEntity responseEntity = RequestSp.GetHttpEntity(_updateUrl);
            if (responseEntity == null || responseEntity.getContentLength() <= 0) {
                return null;
            }
            String responseStr = EntityUtils.toString(responseEntity, "UTF-8");
            if (responseStr == null || responseStr.length() <= 0) {
                return null;
            }
            JSONArray jsonObjs = new JSONObject(responseStr).getJSONArray(UPDATE_KEYS.ITEMS);
            if (jsonObjs == null || jsonObjs.length() <= 0) {
                return null;
            }
            int itemCount = jsonObjs.length();
            updateItems = new ArrayList<UpdateItem>();
            for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
                JSONObject jsonObject = jsonObjs.getJSONObject(itemIndex);
                UpdateItem updateItem = new UpdateItem();
                updateItem.SetName(jsonObject.getString(UPDATE_KEYS.NAME));
                updateItem.SetFeaturePackage(jsonObject.getString(UPDATE_KEYS.FEATURE_PACKAGE));
                updateItem.SetNewVersion(jsonObject.getString(UPDATE_KEYS.VERSION));
                updateItem.SetUrl(jsonObject.getString(UPDATE_KEYS.URL));
                updateItems.add(updateItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return updateItems;
        }
    }
}

重點說明:

  1. 更新列表的 URL 預設為 https://192.168.1.5:9000/update.json,其中前者為伺服器位址,update.json 為資料檔。
  2. RequestSp.GetHttpEntity 的實作後文會給出。
  3. 網路傳輸一般以 UTF-8 為預設編碼,取得 HttpEntity 後以 UTF-8 轉為字串。
  4. 使用 JSONArray、JSONObject、getJSONArray 取得具體資料,getJSONArray 的參數為 json 物件名稱,集中定義在 UPDATE_KEYS 類中。
  5. 再以 jsonObject.getString 依每項的 key 取值並賦給 updateItem,最終形成 updateItems。

UPDATE_KEYS.java

package com.example.apkupdate;

public class UPDATE_KEYS {
    public final static String ITEMS = "Items";
    public final static String NAME = "Name";
    public final static String FEATURE_PACKAGE = "FeaturePackage";
    public final static String VERSION = "Version";
    public final static String URL = "Url";
}

RequestSp.java

package com.kitsp.httpsp;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class RequestSp {
    private final static int HTTP_200 = 200;

    public static HttpEntity GetHttpEntity(String url) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResp = httpClient.execute(httpGet);
        if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
            return httpResp.getEntity();
        } else {
            return null;
        }
    }
}

除了 JSON,也可以用 XML 傳輸,只是較為麻煩,需要搭配 SAX 解析。取得更新列表資料後,介面上的列表就有內容了。至於如何下載與安裝,請參見後續〈Android 基於 IIS 的 APK 下載(四)數據下載〉。完整程式碼可於作者的 GitHub 專案 ApkUpdate 取得。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved