2012年4月14日 星期六

[Android]臺灣銀行牌告匯率(全部幣別)


<ExchangeRateQueryAll.java>
   1: package com.android.exchangeratequeryall;
   2:  
   3: import java.io.BufferedReader;
   4: import java.io.InputStreamReader;
   5: import java.net.HttpURLConnection;
   6: import java.net.URL;
   7: import java.util.ArrayList;
   8: import java.util.HashMap;
   9:  
  10: import android.app.Activity;
  11: import android.os.Bundle;
  12: import android.util.Log;
  13: import android.view.View;
  14: import android.widget.Button;
  15: import android.widget.ListView;
  16: import android.widget.SimpleAdapter;
  17: import android.widget.TextView;
  18:  
  19: public class ExchangeRateQueryAll extends Activity {
  20:  
  21:     Button btnUpdate, btnExit;
  22:     TextView tvInformationTime;
  23:     ListView lvExchangeRate;
  24:     SimpleAdapter laSimple2;
  25:     String[] arrayCurrency;
  26:     String strRateInformation; 
  27:     ArrayList<HashMap<String,String>> listItem = new ArrayList<HashMap<String,String>>();
  28:  
  29:     int itemIndex, start, end, counter; 
  30:     URL url;
  31:     HttpURLConnection httpUrlconnection;
  32:     String WebURL = "http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm";
  33:     String urlData, informationTime;
  34:     String cashBuyPrice, cashSellPrice, currentBuyPrice, currentSellPrice;
  35:     
  36:     @Override
  37:     public void onCreate(Bundle savedInstanceState) {
  38:         super.onCreate(savedInstanceState);
  39:         setContentView(R.layout.main);
  40:         findViews();
  41:         getWebInformation();
  42:         setListViewControls();
  43:         setButtonClickListener();
  44:     }
  45:         
  46:     protected void findViews() {
  47:         btnUpdate = (Button) findViewById(R.id.updateBtn);
  48:         btnExit = (Button) findViewById(R.id.exitBtn);
  49:         tvInformationTime = (TextView) findViewById(R.id.informationtimeTextView);
  50:         lvExchangeRate = (ListView) findViewById(R.id.exchangerateListView);
  51:     }
  52:     
  53:     protected void getWebInformation(){
  54:         // 讀取網頁資料,並將資料存放在於 urlData
  55:          urlData = GetURLData();
  56:          // 剖析 urlData,擷取所選擇幣別的匯率資料
  57:              Parser(urlData);
  58:     }
  59:     
  60:  
  61:     protected void setListViewControls() {
  62:         laSimple2 = new SimpleAdapter(    this, 
  63:                                         listItem,
  64:                                         android.R.layout.simple_list_item_2,
  65:                                         new String[] { "Currency","RateInformation" },
  66:                                         new int[] { android.R.id.text1, android.R.id.text2 } );
  67:         lvExchangeRate.setAdapter(laSimple2);
  68:     }
  69:     
  70:     protected void setButtonClickListener() {
  71:  
  72:         btnUpdate.setOnClickListener(new Button.OnClickListener(){
  73:             public void onClick(View v) {
  74:                 getWebInformation();
  75:             } 
  76:         });  
  77:         
  78:         btnExit.setOnClickListener(new Button.OnClickListener(){
  79:             public void onClick(View v) {
  80:                 android.os.Process.killProcess(android.os.Process.myPid());
  81:             } 
  82:         });  
  83:         
  84:     }
  85:     
  86:     public String GetURLData(){
  87:         // 讀取網頁資料,並將資料存放在 urlData 
  88:         urlData = null; 
  89:         try { 
  90:              url = new URL(WebURL); 
  91:             httpUrlconnection = (HttpURLConnection) url.openConnection(); 
  92:              httpUrlconnection.setDoInput(true); 
  93:             httpUrlconnection.setDoOutput(true); 
  94:             httpUrlconnection.connect();
  95:             BufferedReader bufReader = new BufferedReader(
  96:                                         new InputStreamReader(httpUrlconnection.getInputStream())); 
  97:             String decodedString; 
  98:             while ((decodedString = bufReader.readLine()) != null) { 
  99:                 urlData += decodedString; 
 100:             } 
 101:             bufReader.close();
 102:             httpUrlconnection.disconnect();
 103:         } 
 104:         catch(Exception e){ 
 105:             Log.e("ERROR", e.toString()); 
 106:         } 
 107:         return urlData;
 108:     } 
 109:     
 110:     
 111:     public void Parser( String urlData ){
 112:         arrayCurrency = getResources().getStringArray(R.array.money);
 113:         // 剖析 urlData,擷取所選擇幣別的匯率資料
 114:         String temp = null;
 115:         // 擷取【牌價最新掛牌時間】
 116:         end = 0; 
 117:         start =  urlData.indexOf("牌價最新掛牌時間:", end+1);
 118:         end =  urlData.indexOf("</td>", start+1);
 119:         informationTime = urlData.substring(start+15, end);
 120:         tvInformationTime.setText("牌價最新掛牌時間:"+informationTime);
 121:         // 從第一個幣別匯率資料開始擷取,直到所選擇的幣別匯率資料為止
 122:         listItem.clear();
 123:         for (counter = 0 ; counter < 19 ; counter++)
 124:         {
 125:             strRateInformation = "";
 126:             // 擷取【現金匯率-買入】價格資料
 127:             start =  urlData.indexOf("<td class=\"decimal\">", end+1); 
 128:             end =  urlData.indexOf("</td>", start+1);
 129:             temp = urlData.substring(start+20, end);
 130:             if ( !temp.equals("-") )
 131:                 cashBuyPrice = temp;
 132:             else
 133:                 cashBuyPrice = "無資料";
 134:             strRateInformation = "【現金匯率】買入:" + cashBuyPrice;
 135:             // 擷取【現金匯率-賣出】價格資料
 136:             start =  urlData.indexOf("<td class=\"decimal\">", end+1); 
 137:             end =  urlData.indexOf("</td>", start+1); 
 138:             temp = urlData.substring(start+20, end); 
 139:             if ( !temp.equals("-") ) 
 140:                 cashSellPrice = temp;
 141:             else
 142:                 cashSellPrice = "無資料";
 143:             strRateInformation += "  賣出:" + cashSellPrice;
 144:             // 擷取【即期匯率-買入】價格資料
 145:             start =  urlData.indexOf("<td class=\"decimal\">", end+1); 
 146:             end =  urlData.indexOf("</td>", start+1); 
 147:             temp = urlData.substring(start+20, end); 
 148:             if ( !temp.equals("-") )
 149:                 currentBuyPrice = temp;
 150:             else
 151:                 currentBuyPrice  = "無資料";
 152:             strRateInformation += "\n【即期匯率】買入:" + currentBuyPrice;
 153:             // 擷取【即期匯率-賣出】價格資料
 154:             start =  urlData.indexOf("<td class=\"decimal\">", end+1); 
 155:             end =  urlData.indexOf("</td>", start+1); 
 156:             temp = urlData.substring(start+20, end); 
 157:             if ( !temp.equals("-") )
 158:                 currentSellPrice = temp;
 159:             else
 160:                 currentSellPrice  = "無資料";
 161:             strRateInformation += "  賣出:" + currentSellPrice;
 162:             // put data into listItem
 163:             HashMap<String,String> item = new HashMap<String,String>();
 164:             item.put( "Currency", arrayCurrency[counter]);
 165:             item.put( "RateInformation",strRateInformation);
 166:             listItem.add( item );
 167:         } 
 168:     }
 169:  
 170: } 

<main.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:     android:id="@+id/LinearLayout1"
   4:     android:layout_width="match_parent"
   5:     android:layout_height="fill_parent"
   6:     android:orientation="vertical" >
   7:  
   8:     <TextView
   9:         android:id="@+id/textView1"
  10:         android:layout_width="match_parent"
  11:         android:layout_height="wrap_content"
  12:         android:gravity="center"
  13:         android:text="@string/strTitle"
  14:         android:textSize="24dp" />
  15:  
  16:     <TextView
  17:         android:id="@+id/informationtimeTextView"
  18:         android:layout_width="match_parent"
  19:         android:layout_height="wrap_content"
  20:         android:text="@string/strInformationTime" />
  21:  
  22:     <TextView
  23:         android:id="@+id/textView2"
  24:         android:layout_width="wrap_content"
  25:         android:layout_height="wrap_content"
  26:         android:text="@string/strInformationSource" />
  27:  
  28:     <LinearLayout
  29:         android:id="@+id/linearLayout2"
  30:         android:layout_width="match_parent"
  31:         android:layout_height="wrap_content" >
  32:  
  33:         <Button
  34:             android:id="@+id/updateBtn"
  35:             android:layout_width="wrap_content"
  36:             android:layout_height="wrap_content"
  37:             android:text="@string/strUpdate" />
  38:  
  39:         <Button
  40:             android:id="@+id/exitBtn"
  41:             android:layout_width="wrap_content"
  42:             android:layout_height="wrap_content"
  43:             android:text="@string/strExit" />
  44:  
  45:     </LinearLayout>
  46:  
  47:     <ListView
  48:         android:id="@+id/exchangerateListView"
  49:         android:layout_width="match_parent"
  50:         android:layout_height="wrap_content" >
  51:  
  52:     </ListView>
  53:  
  54: </LinearLayout>

<strings.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <resources>
   3:  
   4:     <string name="hello">Hello World, ExchangeRateQueryAll!</string>
   5:     <string name="app_name">匯率(全部)</string>
   6:     <string name="strTitle">臺灣銀行牌告匯率</string>
   7:     <string name="strInformationSource">資料來源:臺灣銀行</string>
   8:     <string name="strExit">離開</string>
   9:     <string name="strInformationTime">牌價最新掛牌時間:</string>
  10:     <string name="strUpdate">取得最新報價資料</string>
  11:     
  12: </resources>

<arrays.xml>
   1: <?xml version="1.0" encoding="UTF-8"?>
   2: <resources>
   3:     <string-array name="money">
   4:         <item>美金(USD)</item>
   5:         <item>港幣(HKD)</item>
   6:         <item>英鎊(GBP)</item>
   7:         <item>澳幣(AUD)</item>
   8:         <item>加拿大幣(CAD)</item>
   9:         <item>新加坡幣(SGD)</item>
  10:         <item>瑞士法郎(CHF)</item>
  11:         <item>日圓(JPY)</item>
  12:         <item>南非幣(ZAR)</item>
  13:         <item>瑞典幣(SEK)</item>
  14:         <item>紐元(NZD)</item>
  15:         <item>泰幣(THB)</item>
  16:         <item>菲國比索(PHP)</item>
  17:         <item>印尼幣(IDR)</item>
  18:         <item>歐元(EUR)</item>
  19:         <item>韓元(KRW)</item>
  20:         <item>越南盾(VND)</item>
  21:         <item>馬來幣(MYR)</item>
  22:         <item>人民幣(CNY)</item>
  23:     </string-array>
  24: </resources>

<AndroidManifest.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   3:     package="com.android.exchangeratequeryall"
   4:     android:versionCode="1"
   5:     android:versionName="1.0" android:installLocation="auto">
   6:  
   7:     <uses-sdk android:minSdkVersion="8" />
   8:     <uses-permission android:name="android.permission.INTERNET"/>
   9:  
  10:     <application
  11:         android:icon="@drawable/currency"
  12:         android:label="@string/app_name" >
  13:         <activity
  14:             android:name=".ExchangeRateQueryAll"
  15:             android:label="@string/app_name" >
  16:             <intent-filter>
  17:                 <action android:name="android.intent.action.MAIN" />
  18:  
  19:                 <category android:name="android.intent.category.LAUNCHER" />
  20:             </intent-filter>
  21:         </activity>
  22:     </application>
  23:  
  24: </manifest>

沒有留言:

張貼留言