Use Internet Data in Android applications (1)

Source: Internet
Author: User

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:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <LinearLayout android:orientation="horizontal"   
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"> 
  10.         <EditText android:id="@+id/symbol" android:layout_width="wrap_content" 
  11.             android:layout_height="wrap_content" android:width="120dip"/> 
  12.         <Button android:id="@+id/addBtn" android:layout_width="wrap_content" 
  13.             android:layout_height="wrap_content"   
  14.             android:text="@string/addBtnLbl"/> 
  15.     </LinearLayout> 
  16.     <LinearLayout android:orientation="horizontal"   
  17.         android:layout_width="fill_parent"   
  18.         android:layout_height="wrap_content"> 
  19.         <TextView android:layout_width="wrap_content"   
  20.             android:layout_height="wrap_content" android:id="@+id/symList" /> 
  21.         <Button android:id="@+id/dlBtn" android:layout_width="wrap_content"   
  22.             android:layout_height="wrap_content"   
  23.             android:text="@string/dlBtnLbl" 
  24.         /> 
  25.        </LinearLayout> 
  26.     <ListView android:id="@android:id/list"   
  27.         android:layout_height="fill_parent" android:layout_width="fill_parent" 
  28.         android:layout_weight="1" 
  29.         /> 
  30. </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

 
 
  1. public class Main extends ListActivity {  
  2.     private int mode = XML; // default  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         final EditText input = (EditText) findViewById(R.id.symbol);  
  8.         final TextView symbolsList = (TextView) findViewById(R.id.symList);  
  9.         final Button addButton = (Button) findViewById(R.id.addBtn);  
  10.         final Button dlButton = (Button) findViewById(R.id.dlBtn);  
  11.         addButton.setOnClickListener(new OnClickListener(){  
  12.             public void onClick(View v) {  
  13.                 String newSymbol = input.getText().toString();  
  14.                 if (symbolsList.getText() == null ||   
  15.                         symbolsList.getText().length() == 0){  
  16.                     symbolsList.setText(newSymbol);  
  17.                 } else {  
  18.                     StringBuilder sb =   
  19.                                   new StringBuilder(symbolsList.getText());  
  20.                     sb.append(",");  
  21.                     sb.append(newSymbol);  
  22.                     symbolsList.setText(sb.toString());  
  23.                 }  
  24.                 input.setText("");  
  25.             }  
  26.         });  
  27.         dlButton.setOnClickListener(new OnClickListener(){  
  28.             public void onClick(View v) {  
  29.                 String symList = symbolsList.getText().toString();  
  30.                 String[] symbols = symList.split(",");  
  31.                 symbolsList.setText("");  
  32.                 switch (mode){  
  33.                 case JSON :  
  34.                     new StockJsonParser().execute(symbols);  
  35.                     break;  
  36.                 case PROTOBUF :  
  37.                     new StockProtoBufParser().execute(symbols);  
  38.                     break;  
  39.                 default :  
  40.                     new StockXmlParser().execute(symbols);  
  41.                     break;  
  42.                 }  
  43.             }  
  44.         }  
  45.     }  

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:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.