Android 应用程序必须访问位于 Internet 上的数据,而 Internet 数据可以有几种不同的格式。本文将介绍在 Android 应用程序中如何使用三种数据格式:
- XML
- JSON
- Google 的 protocol buffers
首先开发一个 Web 服务,将 CSV 数据转换成 XML、JSON 和 protocol-buffers 格式。然后构建一个样例 Android 应用程序,可以从 Web 服务中以任何一种格式提取数据并将其解析并显示给用户。
要进行本文中的练习,您需要最新的 Android SDK(参见
您还需要一个 Java web 应用程序服务器来运行 Android 应用程序使用的 Web 服务。此外,也可以将服务器端代码部署到 Google App Engine。参见
您将开发一个简单的 Android 应用程序,叫做 Day Trader。Day Trader 允许用户输入一个或更多的股票代码并获取其所代表股票的最新价格信息。用户可以指定数据使用的格式:XML、JSON 或 protocol buffers。实际的 Android 应用程序通常不会提供此选择,但是通过实现此功能,您可以了解如何让您的应用程序处理每一种格式。图 1
文本框及其旁边的
清单 1
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://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
Activity
:
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
symList TextView
symList TextView
mode
mode