Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

在 Android 應用中處理網路數據(上):Day Trader 應用程式的誕生

編輯:開發入門

Android 應用程式往往需要存取位於網際網路另一端上的資料,而這些資料可能以好幾種不同的格式存在。本文要帶你動手實作,看看如何在 Android 應用裡妥善使用以下三種資料格式:

  • XML
  • JSON
  • Google 的 protocol buffers

我們會先打造一個 Web 服務,把 CSV 資料轉換成 XML、JSON 與 protocol-buffers 三種格式;接著再建構一個範例 Android 應用,讓它能以任一种格式從這個服務抓取資料、解析並呈現給使用者。

要進行本文的實作,你需要最新的 Android SDK(見 參考資料)與 Android 2.2 平台。SDK 還要求安裝一套 Java™ 開發套件(JDK),本文使用的是 JDK 1.6.0_17。你不需要真的準備一台 Android 實機,所有程式碼都能在 SDK 附帶的 Android 模擬器中運行。本文並非 Android 開發入門教學,因此建議你先具備一些 Android 程式設計基礎;當然,光憑 Java 語言的底子也能讀懂本文內容。

此外,你還需要一台 Java Web 應用程式伺服器來運行應用程式所仰賴的 Web 服務,也可以把伺服器端程式碼部署到 Google App Engine。完整原始碼可從 下載 一節取得。

Day Trader 應用程式

我們要開發的這個簡單 Android 應用叫做 Day Trader。它讓使用者輸入一或多個股票代碼,抓取對應股票的最新報價。使用者還能指定資料格式:XML、JSON 或 protocol buffers。真實的應用程式通常不會把這種選擇攤在陽光下,但藉由實作它,你可以徹底搞懂如何讓自己的應用程式應付每一種格式。圖 1 展示了 Day Trader 的使用者介面:

圖 1. 運行中的 Day Trader 應用程式

文字方塊和它旁邊的 Add Stock 按鈕,讓使用者逐一輸入想關注的股票代碼。按下 Download Stock Data 後,應用程式會向伺服器請求這些股票的資料,把它們解析並顯示在螢幕上。預設抓的是 XML 資料;透過選單,你可以在 XML、JSON 與 protocol buffers 之間切換格式。

清單 1 呈現了建構 圖 1 介面所用的版面配置 XML:

清單 1. Day Trader 版面配置 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <EditText android:id="@+id/symbol"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="120dip"/>
        <Button android:id="@+id/addBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/addBtnLbl"/>
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/symList" />
        <Button android:id="@+id/dlBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dlBtnLbl" />
    </LinearLayout>
    <ListVIEw android:id="@android:id/list"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1" />
</LinearLayout>

這份配置檔大多一目了然。幾個小元件就湊出了 圖 1 的輸入欄與按鈕;此外還有一個 ListView——堪稱 Android 元件裡的瑞士軍刀。這個 ListView 將用伺服器下載的股票資料來填滿。清單 2 展示了掌控這個視圖的 Activity:

清單 2. Day Trader 主活動

public class Main extends ListActivity {
  private int mode = XML; // default
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText input = (EditText) findViewById(R.id.symbol);
    final TextView symbolsList = (TextView) findViewById(R.id.symList);
    final Button addButton = (Button) findViewById(R.id.addBtn);
    final Button dlButton = (Button) findViewById(R.id.dlBtn);
    addButton.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        String newSymbol = input.getText().toString();
        if (symbolsList.getText() == null || symbolsList.getText().length() == 0){
          symbolsList.setText(newSymbol);
        } else {
          StringBuilder sb = new StringBuilder(symbolsList.getText());
          sb.append(",");
          sb.append(newSymbol);
          symbolsList.setText(sb.toString());
        }
        input.setText("");
      }
    });
    dlButton.setOnClickListener(new OnClickListener(){
      public void onClick(VIEw v) {
        String symList = symbolsList.getText().toString();
        String[] symbols = symList.split(",");
        symbolsList.setText("");
        switch (mode){
          case JSON : new StockJSonParser().execute(symbols); break;
          case PROTOBUF : new StockProtoBufParser().execute(symbols); break;
          default : new StockXMLParser().execute(symbols); break;
        }
      }
    });
  }
}

這個 Activity 載入了 清單 1 的版面配置,並把幾個事件處理器串接起來。先看 Add Stock 按鈕:程式碼讀取文字方塊中的代碼,再用逗號分隔,把它附加到 symList TextView 裡。再看 Download 按鈕:處理器從 symList TextView 讀出資料,然後依 mode 變數,選用三個不同的類之一向伺服器下載資料。而 mode 的值由選單設定;由於這段程式碼並不關鍵,我在 清單 2 中略去了它。在深入這些下載/解析類之前,先讓你看看伺服器這端是如何供應資料的。

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