自己写的一个在线汇率转换工具-MoneyDroid sources

已经把更新的项目SVN到SF上了,地址: 复制我

先main.xml:

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android: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 stub sp1 = 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 stub sp2 = 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()); // 为输出创建BufferedReader BufferedReader 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里加入网络权限,东西写的太简陋,正则很慢,很费流量,更好的办法应该是更新汇率到本地数据库,然后本地来计算汇率。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值