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

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

 

 

先main.xml:

 

然后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 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(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("(.+?)(.+?)(.+?)",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里加入网络权限,东西写的太简陋,正则很慢,很费流量,更好的办法应该是更新汇率到本地数据库,然后本地来计算汇率。

 

你可能感兴趣的:(Android)