自己写的一个在线汇率转换工具-MoneyDroid sources
已经把更新的项目SVN到SF上了,地址: 复制我
先main.xml:
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><Spinner android:layout_height="wrap_content" android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_x="16dip" android:layout_y="13dip"></Spinner><Spinner android:layout_height="wrap_content" android:id="@+id/Spinner02" android:layout_width="wrap_content" android:layout_x="133dip" android:layout_y="14dip"></Spinner><EditText android:layout_height="wrap_content" android:layout_width="170px" android:text="" android:textSize="18sp" android:id="@+id/EditText01" android:layout_x="13dip" android:layout_y="101dip"></EditText><Button android:layout_height="wrap_content" android:text="转换" android:layout_width="wrap_content" android:id="@+id/Button01" android:layout_x="240dip" android:layout_y="98dip"></Button><TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView01" android:layout_x="113dip" android:layout_y="220dip"></TextView></AbsoluteLayout>
然后sources:
package com.overflow.moneydroid;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.regex.Matcher;import java.util.regex.Pattern;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.Spinner;import android.widget.TextView;public class Activity01 extends Activity {private final String DEBUG_TAG = "MoneyDroid";private static final String[] moneyName = {"CNY","HKD","TWD","EUR","USD","GBP","AUD","KRW","JPY"};private TextView textview = null;private Button button = null;private Spinner spinner1 = null;private Spinner spinner2 = null;private EditText edittext = null;static String sp1 = "";static String sp2 = "";static String edt = ""; private ArrayAdapter<String> adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textview = (TextView) this.findViewById(R.id.TextView01); button = (Button) this.findViewById(R.id.Button01); spinner1 = (Spinner) this.findViewById(R.id.Spinner01); spinner2 = (Spinner) this.findViewById(R.id.Spinner02); edittext = (EditText) this.findViewById(R.id.EditText01); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,moneyName); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(adapter); spinner2.setAdapter(adapter); spinner1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubsp1 = moneyName[arg2];Log.e(DEBUG_TAG, sp1);arg0.setVisibility(View.VISIBLE);}public void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub} }); spinner2.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubsp2 = moneyName[arg2];Log.e(DEBUG_TAG, sp2);arg0.setVisibility(View.VISIBLE);}public void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub} }); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) {// TODO Auto-generated method stub//textview.setText("ok");String httpUrl = "http://www.123cha.com/hl/?q="+edittext.getText().toString()+"&from="+sp1+"&to="+sp2+"&s="+sp1+sp2+"#symbol="+sp1+sp2+"=X;range=3m;";//构造一个URL对象String resultData = ""; URL url = null;try{url = new URL(httpUrl); }catch (MalformedURLException e){Log.e(DEBUG_TAG, "MalformedURLException");}if (url != null){try{// 使用HttpURLConnection打开连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();//得到读取的内容(流)InputStreamReader in = new InputStreamReader(urlConn.getInputStream());// 为输出创建BufferedReaderBufferedReader buffer = new BufferedReader(in);String inputLine = null;String matcherLine = null;//使用循环来读取获得的数据Pattern pattern = Pattern.compile("<td>(.+?)</td><td>(.+?)</td><td>(.+?)</td>",Pattern.DOTALL);while (((inputLine = buffer.readLine()) != null)){//我们在每一行后面加上一个"\n"来换行resultData += inputLine + "\n";Matcher matcher = pattern.matcher(resultData);if (matcher.find()) {Log.e(DEBUG_TAG, matcher.group());matcherLine = matcher.group(3).trim();}} //关闭InputStreamReader//Log.e(DEBUG_TAG,httpUrl);in.close();//关闭http连接urlConn.disconnect();//设置显示取得的内容if ( matcherLine != null ){textview.setText(matcherLine);}else {textview.setText("读取的内容为NULL");}}catch (IOException e){Log.e(DEBUG_TAG, "IOException");}}else{Log.e(DEBUG_TAG, "Url NULL");}} }); }}
要在manifest.xml里加入网络权限,东西写的太简陋,正则很慢,很费流量,更好的办法应该是更新汇率到本地数据库,然后本地来计算汇率。