<ExchangeRateQuery.java>
1: package com.android.ExchangeRateQuery;
2:
3: import java.io.BufferedReader;
4: import java.io.InputStreamReader;
5: import java.net.HttpURLConnection;
6: import java.net.URL;
7: import com.android.R;
8:
9: import android.app.Activity;
10: import android.os.Bundle;
11: import android.util.Log;
12: import android.view.View;
13: import android.widget.AdapterView;
14: import android.widget.ArrayAdapter;
15: import android.widget.Button;
16: import android.widget.Spinner;
17: import android.widget.TextView;
18:
19: public class ExchangeRateQuery extends Activity {
20:
21: Spinner mSpinner;
22: Button btnExit;
23: TextView tvCashBuy, tvCashSell, tvCurrentBuy, tvCurrentSell, tvInformationTime;
24: int itemIndex, start, end, counter;
25: URL url;
26: HttpURLConnection httpUrlconnection;
27: String WebURL = "http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm";
28: String urlData, informationTime;
29: String cashBuyPrice, cashSellPrice, currentBuyPrice, currentSellPrice;
30:
31: @Override
32: public void onCreate(Bundle savedInstanceState) {
33: super.onCreate(savedInstanceState);
34: setContentView(R.layout.main);
35: findViews();
36: setSpinnerControls();
37: setSpinnerSelectListener();
38: setButtonClickListener();
39: }
40:
41: protected void findViews() {
42: mSpinner = (Spinner) findViewById(R.id.spinner1);
43: btnExit = (Button) findViewById(R.id.exitBtn);
44: tvCashBuy = (TextView) findViewById(R.id.cashbuyTextView);
45: tvCashSell = (TextView) findViewById(R.id.cashsellTextView);
46: tvCurrentBuy = (TextView) findViewById(R.id.currentbuyTextView);
47: tvCurrentSell = (TextView) findViewById(R.id.currentsellTextView);
48: tvInformationTime = (TextView) findViewById(R.id.informationtimeTextView);
49: }
50:
51: protected void setSpinnerControls(){
52: ArrayAdapter<?> adapter = ArrayAdapter.createFromResource( this,
53: R.array.money,
54: android.R.layout.simple_spinner_item);
55: adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
56: mSpinner.setAdapter(adapter);
57: }
58:
59: protected void setSpinnerSelectListener() {
60: mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
61: public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
62: itemIndex = arg2;
63: // 讀取網頁資料,並將資料存放在於 urlData
64: urlData = GetURLData();
65: // 剖析 urlData,擷取所選擇幣別的匯率資料
66: Parser(urlData);
67: // 將擷取到所選擇的幣別匯率資料顯示於 TextView 欄位中
68: showResults();
69: }
70:
71: public void onNothingSelected(AdapterView<?> arg0) {
72:
73: }
74: });
75: }
76:
77: protected void setButtonClickListener() {
78:
79: btnExit.setOnClickListener(new Button.OnClickListener(){
80: public void onClick(View v) {
81: android.os.Process.killProcess(android.os.Process.myPid());
82: }
83: });
84:
85: }
86:
87: public String GetURLData(){
88: // 讀取網頁資料,並將資料存放在 urlData
89: urlData = null;
90: try {
91: url = new URL(WebURL);
92: httpUrlconnection = (HttpURLConnection) url.openConnection();
93: httpUrlconnection.setDoInput(true);
94: httpUrlconnection.setDoOutput(true);
95: httpUrlconnection.connect();
96: BufferedReader bufReader = new BufferedReader(
97: new InputStreamReader(httpUrlconnection.getInputStream()));
98: String decodedString;
99: while ((decodedString = bufReader.readLine()) != null) {
100: urlData += decodedString;
101: }
102: bufReader.close();
103: httpUrlconnection.disconnect();
104: }
105: catch(Exception e){
106: Log.e("ERROR", e.toString());
107: }
108: return urlData;
109: }
110:
111:
112: public void Parser( String urlData ){
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: // 從第一個幣別匯率資料開始擷取,直到所選擇的幣別匯率資料為止
121: for (counter = 0 ; counter <= itemIndex ; counter++)
122: {
123: // 擷取【現金匯率-買入】價格資料
124: start = urlData.indexOf("<td class=\"decimal\">", end+1);
125: end = urlData.indexOf("</td>", start+1);
126: temp = urlData.substring(start+20, end);
127: if ( !temp.equals("-") )
128: cashBuyPrice = temp;
129: else
130: cashBuyPrice = "無資料";
131: // 擷取【現金匯率-賣出】價格資料
132: start = urlData.indexOf("<td class=\"decimal\">", end+1);
133: end = urlData.indexOf("</td>", start+1);
134: temp = urlData.substring(start+20, end);
135: if ( !temp.equals("-") )
136: cashSellPrice = temp;
137: else
138: cashSellPrice = "無資料";
139: // 擷取【即期匯率-買入】價格資料
140: start = urlData.indexOf("<td class=\"decimal\">", end+1);
141: end = urlData.indexOf("</td>", start+1);
142: temp = urlData.substring(start+20, end);
143: if ( !temp.equals("-") )
144: currentBuyPrice = temp;
145: else
146: currentBuyPrice = "無資料";
147: // 擷取【即期匯率-賣出】價格資料
148: start = urlData.indexOf("<td class=\"decimal\">", end+1);
149: end = urlData.indexOf("</td>", start+1);
150: temp = urlData.substring(start+20, end);
151: if ( !temp.equals("-") )
152: currentSellPrice = temp;
153: else
154: currentSellPrice = "無資料";
155: }
156: }
157:
158: public void showResults() {
159: // 將擷取到所選擇的幣別匯率資料顯示於 TextView 欄位中
160: tvCashBuy.setText(cashBuyPrice);
161: tvCashSell.setText(cashSellPrice+"\n");
162: tvCurrentBuy.setText(currentBuyPrice);
163: tvCurrentSell.setText(currentSellPrice+"\n");
164: tvInformationTime.setText("\n牌價最新掛牌時間:"+informationTime+"\n");
165: }
166: }
<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: <LinearLayout
9: android:id="@+id/linearLayout2"
10: android:layout_width="match_parent"
11: android:layout_height="wrap_content" >
12:
13: <Spinner
14: android:id="@+id/spinner1"
15: android:layout_width="0dp"
16: android:layout_height="wrap_content"
17: android:layout_weight="1" />
18:
19: <TextView
20: android:id="@+id/textView1"
21: android:layout_width="wrap_content"
22: android:layout_height="wrap_content"
23: android:gravity="right"
24: android:text="@string/strTitle"
25: android:textSize="18dp" />
26:
27: </LinearLayout>
28:
29: <LinearLayout
30: android:id="@+id/linearLayout3"
31: android:layout_width="match_parent"
32: android:layout_height="wrap_content"
33: android:orientation="vertical" >
34:
35: <TextView
36: android:id="@+id/textView2"
37: android:layout_width="wrap_content"
38: android:layout_height="wrap_content"
39: android:text="@string/strCashRate"
40: android:textSize="20dp" />
41:
42: </LinearLayout>
43:
44: <LinearLayout
45: android:id="@+id/linearLayout4"
46: android:layout_width="match_parent"
47: android:layout_height="wrap_content" >
48:
49:
50: <TextView
51: android:id="@+id/textView3"
52: android:layout_width="wrap_content"
53: android:layout_height="wrap_content"
54: android:text="@string/strBuy" />
55:
56: <TextView
57: android:id="@+id/cashbuyTextView"
58: android:layout_width="wrap_content"
59: android:layout_height="wrap_content"
60: android:text="@string/strNull" />
61:
62: </LinearLayout>
63:
64: <LinearLayout
65: android:id="@+id/linearLayout5"
66: android:layout_width="match_parent"
67: android:layout_height="wrap_content" >
68:
69: <TextView
70: android:id="@+id/textView5"
71: android:layout_width="wrap_content"
72: android:layout_height="wrap_content"
73: android:text="@string/strSell" />
74:
75: <TextView
76: android:id="@+id/cashsellTextView"
77: android:layout_width="wrap_content"
78: android:layout_height="wrap_content"
79: android:text="@string/strNull" />
80:
81: </LinearLayout>
82:
83: <LinearLayout
84: android:id="@+id/linearLayout6"
85: android:layout_width="match_parent"
86: android:layout_height="wrap_content"
87: android:orientation="vertical" >
88:
89: <TextView
90: android:id="@+id/textView7"
91: android:layout_width="wrap_content"
92: android:layout_height="wrap_content"
93: android:text="@string/strCurrentRate"
94: android:textSize="20dp" />
95:
96: </LinearLayout>
97:
98: <LinearLayout
99: android:id="@+id/linearLayout7"
100: android:layout_width="match_parent"
101: android:layout_height="wrap_content" >
102:
103:
104: <TextView
105: android:id="@+id/textView8"
106: android:layout_width="wrap_content"
107: android:layout_height="wrap_content"
108: android:text="@string/strBuy" />
109:
110: <TextView
111: android:id="@+id/currentbuyTextView"
112: android:layout_width="wrap_content"
113: android:layout_height="wrap_content"
114: android:text="@string/strNull" />
115:
116: </LinearLayout>
117:
118: <LinearLayout
119: android:id="@+id/linearLayout8"
120: android:layout_width="match_parent"
121: android:layout_height="wrap_content" >
122:
123: <TextView
124: android:id="@+id/textView10"
125: android:layout_width="wrap_content"
126: android:layout_height="wrap_content"
127: android:text="@string/strSell" />
128:
129: <TextView
130: android:id="@+id/currentsellTextView"
131: android:layout_width="wrap_content"
132: android:layout_height="wrap_content"
133: android:text="@string/strNull" />
134:
135: </LinearLayout>
136:
137: <Button
138: android:id="@+id/exitBtn"
139: android:layout_width="wrap_content"
140: android:layout_height="wrap_content"
141: android:text="@string/strExit" />
142:
143: <TextView
144: android:id="@+id/informationtimeTextView"
145: android:layout_width="wrap_content"
146: android:layout_height="wrap_content"
147: android:text="@string/strInformationTime" />
148:
149: <TextView
150: android:id="@+id/textView13"
151: android:layout_width="wrap_content"
152: android:layout_height="wrap_content"
153: android:text="@string/strInformationSource" />
154:
155: <TextView
156: android:id="@+id/textView4"
157: android:layout_width="match_parent"
158: android:layout_height="wrap_content"
159: android:gravity="center"
160: android:text="@string/strCopyright" />
161:
162: </LinearLayout>
<strings.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <resources>
3:
4: <string name="hello">Hello World, ExchangeRateQueryActivity!</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="strCurrency">請選擇幣別</string>
10: <string name="strCashRate">現金匯率</string>
11: <string name="strCurrentRate">即期匯率</string>
12: <string name="strBuy">買入:</string>
13: <string name="strSell">賣出:</string>
14: <string name="strNull"></string>
15: <string name="strInformationTime">牌價最新掛牌時間:</string>
16: <string name="strCopyright">\nCopyright : Terry Wu (terry.wuteyu@gmail.com)</string>
17:
18: </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"
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=".ExchangeRateQuery.ExchangeRateQuery"
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>
沒有留言:
張貼留言