2012年4月15日 星期日

[Android]黃金牌價(臺灣銀行黃金存摺牌價、台灣地區銀樓公會即時黃金牌價)


<GoldPrice.java>
   1: package com.android.goldprice;
   2:  
   3: import java.io.BufferedReader;
   4: import java.io.InputStreamReader;
   5: import java.net.HttpURLConnection;
   6: import java.net.URL;
   7:  
   8: import android.app.Activity;
   9: import android.os.Bundle;
  10: import android.util.Log;
  11: import android.view.View;
  12: import android.widget.Button;
  13: import android.widget.TextView;
  14:  
  15: public class GoldPrice extends Activity {
  16:  
  17:     Button btnUpdate, btnExit;
  18:     TextView    tvNTPrice, tvUSPrice, tvGCKPrice,
  19:                 tvNTInformationTime, tvUSInformationTime, tvGCKInformationTime;
  20:     int start, end, counter; 
  21:     URL url;
  22:     HttpURLConnection httpUrlconnection;
  23:     String NTGoldWebURL = "http://rate.bot.com.tw/Pages/Static/UIP005.zh-TW.htm";
  24:     String USGoldWebURL = "http://rate.bot.com.tw/Pages/Static/OBU005.zh-TW.htm";
  25:     String GCKGoldWebURL = "http://www.gck99.com.tw/gold.asp";
  26:     String urlData, informationTime, goldPrice;
  27:     
  28:     @Override
  29:     public void onCreate(Bundle savedInstanceState) {
  30:         super.onCreate(savedInstanceState);
  31:         setContentView(R.layout.main);
  32:         findViews();
  33:         // 顯示新台幣計價黃金存摺牌價
  34:         showNTGoldResults();
  35:         // 顯示美元計價黃金存摺牌價
  36:         showUSGoldResults();
  37:         // 顯示台灣地區即時黃金牌價
  38:         showGCKGoldResults();
  39:         setButtonClickListener();
  40:     }
  41:     
  42:     protected void findViews() {
  43:         btnUpdate  = (Button) findViewById(R.id.updateBtn);
  44:         btnExit = (Button) findViewById(R.id.exitBtn);
  45:         tvNTPrice = (TextView) findViewById(R.id.ntpriceTextView);
  46:         tvUSPrice = (TextView) findViewById(R.id.uspriceTextView);
  47:         tvGCKPrice = (TextView) findViewById(R.id.gckpriceTextView);
  48:         tvNTInformationTime = (TextView) findViewById(R.id.ntinformationtimeTextView);
  49:         tvUSInformationTime = (TextView) findViewById(R.id.usinformationtimeTextView);
  50:         tvGCKInformationTime = (TextView) findViewById(R.id.gckinformationtimeTextView);
  51:     }
  52:  
  53:     public void showNTGoldResults() {
  54:         // 顯示新台幣計價黃金存摺牌價
  55:         // 讀取網頁資料,並將資料存放在於 urlData
  56:          urlData = GetURLData(NTGoldWebURL , 8900);
  57:          // 剖析 urlData,擷取黃金存摺牌價資料
  58:              Parser(urlData);
  59:         // 將擷取到黃金存摺牌價資料顯示於 TextView 欄位中
  60:         tvNTPrice.setText("1公克 "+goldPrice+"\n");
  61:         // 顯示黃金存摺牌價資料掛牌時間
  62:         tvNTInformationTime.setText("掛牌時間:"+informationTime+"\n");
  63:     }
  64:     
  65:     public void showUSGoldResults() {
  66:         // 顯示美元計價黃金存摺牌價
  67:         // 讀取網頁資料,並將資料存放在於 urlData
  68:          urlData = GetURLData(USGoldWebURL , 8900);
  69:          // 剖析 urlData,擷取黃金存摺牌價資料
  70:              Parser(urlData);
  71:         // 將擷取到黃金存摺牌價資料顯示於 TextView 欄位中
  72:         tvUSPrice.setText("1英兩 "+goldPrice+"\n");
  73:         // 顯示黃金存摺牌價資料掛牌時間
  74:         tvUSInformationTime.setText("掛牌時間:"+informationTime+"\n");
  75:     }
  76:     
  77:     public void showGCKGoldResults() {
  78:         // 顯示台灣地區即時黃金牌價
  79:         // 讀取網頁資料,並將資料存放在於 urlData
  80:          urlData = GetURLData(GCKGoldWebURL , 20887);
  81:          // 剖析 urlData,擷取黃金存摺牌價資料
  82:              gckParser(urlData);
  83:         // 將擷取到黃金存摺牌價資料顯示於 TextView 欄位中
  84:         tvGCKPrice.setText("1台錢 "+goldPrice+"\n");
  85:         // 顯示黃金存摺牌價資料掛牌時間
  86:         tvGCKInformationTime.setText("掛牌時間:"+informationTime+"\n");
  87:     }
  88:     
  89:     protected void setButtonClickListener() {
  90:  
  91:         btnUpdate.setOnClickListener(new Button.OnClickListener(){
  92:             public void onClick(View v) {
  93:                 // 顯示新台幣計價黃金存摺牌價
  94:                 showNTGoldResults();
  95:                 // 顯示美元計價黃金存摺牌價
  96:                 showUSGoldResults();
  97:                 // 顯示台灣地區即時黃金牌價
  98:                 showGCKGoldResults();
  99:             } 
 100:         });  
 101:  
 102:         
 103:         btnExit.setOnClickListener(new Button.OnClickListener(){
 104:             public void onClick(View v) {
 105:                 android.os.Process.killProcess(android.os.Process.myPid());
 106:             } 
 107:         });  
 108:         
 109:     }
 110:     
 111:     public String GetURLData(String WebURL, int ReadSize){
 112:         // 讀取網頁資料,並將資料存放在 urlData 
 113:         urlData = null; 
 114:         try { 
 115:              url = new URL(WebURL); 
 116:             httpUrlconnection = (HttpURLConnection) url.openConnection(); 
 117:              httpUrlconnection.setDoInput(true); 
 118:             httpUrlconnection.setDoOutput(true); 
 119:             httpUrlconnection.connect();
 120:             BufferedReader bufReader = new BufferedReader(
 121:                                         new InputStreamReader(httpUrlconnection.getInputStream())); 
 122:             String decodedString; 
 123:             while ((decodedString = bufReader.readLine()) != null) { 
 124:                 urlData += decodedString;
 125:                 if (urlData.length() > ReadSize)
 126:                     break;
 127:             } 
 128:             bufReader.close();
 129:             httpUrlconnection.disconnect();
 130:         } 
 131:         catch(Exception e){ 
 132:             Log.e("ERROR", e.toString()); 
 133:         } 
 134:         return urlData;   
 135:     } 
 136:     
 137:     
 138:     public void Parser( String urlData ){
 139:         // 剖析 urlData,擷取黃金存摺牌價資料
 140:         String temp = null;
 141:         // 擷取【掛牌時間】
 142:         end = 0; 
 143:         start =  urlData.indexOf("掛牌時間:", end+1);
 144:         end =  urlData.indexOf("</td>", start+1);
 145:         informationTime = urlData.substring(start+11, end);
 146:         // 擷取黃金存摺牌價資料
 147:         for (counter = 0 ; counter <= 13 ; counter++)
 148:         {
 149:             start =  urlData.indexOf("<td class=\"decimal\">", end+1); 
 150:             end =  urlData.indexOf("</td>", start+1);
 151:             temp = urlData.substring(start+20, end);
 152:             switch (counter) {
 153:             case 6:
 154:                 goldPrice = "賣出<" + temp + ">元   ";
 155:                 break;
 156:  
 157:             case 13:
 158:                 goldPrice += "買進<" + temp + ">元";
 159:                 break;
 160:                 
 161:             default:
 162:                 break;
 163:             }
 164:         }
 165:      }
 166:     
 167:     public void gckParser( String urlData ){
 168:         // 剖析 GCK urlData,擷取黃金牌價資料
 169:         String temp = null;
 170:         // 擷取【掛牌時間】
 171:         end = 0; 
 172:         start =  urlData.indexOf("<font size=\"1\">", end+1);
 173:         end =  urlData.indexOf("</font>", start+1);
 174:         informationTime = urlData.substring(start+17, end);
 175:         // 擷取黃金牌價資料
 176:         end = 0;
 177:         for (counter = 1 ; counter <= 6 ; counter++)
 178:         {
 179:             start =  urlData.indexOf("font-size:9pt;", end+1); 
 180:             
 181:             switch (counter) {
 182:             case 3:
 183:                 end =  urlData.indexOf("</span>", start+1);
 184:                 temp = urlData.substring(start+19, end);
 185:                 goldPrice = "賣出<" + temp + ">元   ";
 186:                 break;
 187:                 
 188:             case 6:
 189:                 end =  urlData.indexOf("</span>", start+1);
 190:                 temp = urlData.substring(start+19, end);
 191:                 goldPrice = goldPrice + "買進<" + temp + ">元";
 192:                 break;
 193:                 
 194:             default:
 195:                 end =  start + 16;
 196:                 break;
 197:             }
 198:         }
 199:      }
 200: } 

<main.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:     android:layout_width="fill_parent"
   4:     android:layout_height="fill_parent"
   5:     android:orientation="vertical" >
   6:  
   7:     <TextView
   8:         android:id="@+id/textView1"
   9:         android:layout_width="match_parent"
  10:         android:layout_height="wrap_content"
  11:         android:gravity="center"
  12:         android:text="@string/strTitle"
  13:         android:textAppearance="?android:attr/textAppearanceLarge" />
  14:  
  15:     <TextView
  16:         android:id="@+id/textView2"
  17:         android:layout_width="wrap_content"
  18:         android:layout_height="wrap_content"
  19:         android:text="@string/strNTTitle"
  20:         android:textAppearance="?android:attr/textAppearanceMedium" />
  21:  
  22:     <TextView
  23:         android:id="@+id/ntpriceTextView"
  24:         android:layout_width="match_parent"
  25:         android:layout_height="wrap_content"
  26:         android:text="@string/strNTPriceInformation" />
  27:  
  28:     <TextView
  29:         android:id="@+id/ntinformationtimeTextView"
  30:         android:layout_width="match_parent"
  31:         android:layout_height="wrap_content"
  32:         android:text="@string/strInformationTime" />
  33:  
  34:     <TextView
  35:         android:id="@+id/textView3"
  36:         android:layout_width="wrap_content"
  37:         android:layout_height="wrap_content"
  38:         android:text="@string/strUSTitle"
  39:         android:textAppearance="?android:attr/textAppearanceMedium" />
  40:  
  41:     <TextView
  42:         android:id="@+id/textView4"
  43:         android:layout_width="wrap_content"
  44:         android:layout_height="wrap_content"
  45:         android:text="@string/strPS" />
  46:  
  47:     <TextView
  48:         android:id="@+id/uspriceTextView"
  49:         android:layout_width="match_parent"
  50:         android:layout_height="wrap_content"
  51:         android:text="@string/strUSPriceInformation" />
  52:  
  53:     <TextView
  54:         android:id="@+id/usinformationtimeTextView"
  55:         android:layout_width="wrap_content"
  56:         android:layout_height="wrap_content"
  57:         android:text="@string/strInformationTime" />
  58:  
  59:     <TextView
  60:         android:id="@+id/textView5"
  61:         android:layout_width="match_parent"
  62:         android:layout_height="wrap_content"
  63:         android:text="@string/strgckTitle"
  64:         android:textAppearance="?android:attr/textAppearanceMedium" />
  65:  
  66:     <TextView
  67:         android:id="@+id/gckpriceTextView"
  68:         android:layout_width="match_parent"
  69:         android:layout_height="wrap_content"
  70:         android:text="@string/strgckPriceInformation" />
  71:  
  72:     <TextView
  73:         android:id="@+id/gckinformationtimeTextView"
  74:         android:layout_width="wrap_content"
  75:         android:layout_height="wrap_content"
  76:         android:text="@string/strInformationTime" />
  77:  
  78:     <LinearLayout
  79:         android:id="@+id/linearLayout1"
  80:         android:layout_width="match_parent"
  81:         android:layout_height="wrap_content" >
  82:  
  83:         <Button
  84:             android:id="@+id/updateBtn"
  85:             android:layout_width="wrap_content"
  86:             android:layout_height="wrap_content"
  87:             android:text="@string/strUpdate" />
  88:  
  89:         <Button
  90:             android:id="@+id/exitBtn"
  91:             android:layout_width="wrap_content"
  92:             android:layout_height="wrap_content"
  93:             android:text="@string/strExit" />
  94:  
  95:     </LinearLayout>
  96:  
  97:     <TextView
  98:         android:id="@+id/textView6"
  99:         android:layout_width="match_parent"
 100:         android:layout_height="wrap_content"
 101:         android:gravity="center"
 102:         android:text="@string/strCopyright" />
 103:  
 104: </LinearLayout>

<strings.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <resources>
   3:  
   4:     <string name="hello">Hello World, GoldPrice!</string>
   5:     <string name="app_name">黃金牌價</string>
   6:     <string name="strTitle">黃金牌價</string>
   7:     <string name="strNTTitle">臺灣銀行新台幣計價黃金存摺</string>
   8:     <string name="strNTPriceInformation">1公克</string>
   9:     <string name="strUSTitle">臺灣銀行美元計價黃金存摺</string>
  10:     <string name="strUSPriceInformation">1英兩</string>
  11:     <string name="strUpdate">取得最新黃金牌價</string>
  12:     <string name="strExit">離開</string>
  13:     <string name="strPS">【1英兩=31.1公克】</string>
  14:     <string name="strInformationTime">掛牌時間:</string>
  15:     <string name="strgckTitle">台灣地區銀樓公會即時黃金牌價</string>
  16:     <string name="strgckPriceInformation">1台錢</string>
  17:     <string name="strCopyright">\nCopyright : Terry Wu (terry.wuteyu@gmail.com)</string>
  18:  
  19: </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.goldprice"
   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/gold"
  12:         android:label="@string/app_name" >
  13:         <activity
  14:             android:name=".GoldPrice"
  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>

沒有留言:

張貼留言