Android applications must access data on the Internet. Internet data can be in several different formats.. This article describes how to use three data formats in Android applications:
XML
JSONFirst, develop a Web service to convert CSV data into XML, JSON, and protocol-buffers formats. Then, we construct a sample Android Application that extracts data from the Web service in any format and parses the data and displays it to the user.
Google's protocol buffers
To perform the exercises in this article, you need the latest Android SDK, see references) and Android 2.2 platform. The SDK also requires you to install a Java™JDK); JDK 1.6.0 _ 17 is used in this article. You do not need to have an Android physical device; all code will be run in the Android simulator of the SDK. This article does not teach you how to develop Android. Therefore, we recommend that you be familiar with Android programming. Of course, this article can be completed only with the knowledge of the Java programming language.
You also need a Java web application server to run the Web Service used by Android applications. In addition, you can deploy the server code to the Google App Engine. See the download section to obtain the complete source code.
Day Trader Application
You will develop a simple Android Application called Day Trader. Day Trader allows users to enter one or more stock codes and obtain the latest price information about the stocks they represent. You can specify the format of data: XML, JSON, or protocol buffers. The actual Android Application usually does not provide this option, but by implementing this function, you can learn how to let your application process each format. Figure 1 shows the Day Trader user interface:
Figure 1. Running Day Trader Application
The text box and the Add Stock button next to it allow the user to enter the code of each Stock of interest. After you press the Download Stock Data button, the server requests Data of all these stocks, parses the Data in the application, and displays the Data on the screen. By default, XML data is obtained. You can switch between XML, JSON, or protocol buffers data formats through the menu.
Listing 1 shows the layout XML used to create the UI shown in Figure 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>
Most of the above code is simple and clear. The input and buttons shown in Figure 1 are created for several widgets. You will also see a ListView, the real Swiss Army knife in the Android widget. This ListView is filled with stock data downloaded from the server. Listing 2 shows the Activity that controls the View:
Listing 2. Day Trader main 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;
- }
- }
- }
- }
- }
This Activity sets the XML file layout in Listing 1, which connects several event handlers. First, for the Add Stock button, the Code reads the code in the text box and adds it to the symList TextView. Each code is separated by commas. Next, for the Download button, the handler reads data from the symList TextView, and then-based on the mode Variable-uses one of three different classes to Download data from the server. Set the value of the mode variable in the menu; this code is not very important, so I omitted it in Listing 2. Before learning about various data download/Resolution classes, I will show you how the server provides this data. Provide stock data
The application server must be able to do two things. First, it must obtain the stock code list and retrieve their data. Then, it needs to accept a format parameter and encode the data based on the format. For XML and JSON formats, the server returns serialized stock data as text. For protocol buffers, it must send binary data. Listing 3 shows the servlet that handles these steps: