1、取汇率
currencyURL.properties:
AMERICAS=http://www.bloomberg.com/markets/currencies/americas_currencies.html ASIA/PACIFIC=http://www.bloomberg.com/markets/currencies/asiapac_currencies.html EUROPE/AFRICA/MIDDLEEAST=http://www.bloomberg.com/markets/currencies/eurafr_currencies.html YAHOO=http://hk.finance.yahoo.com/currency/convert?amt=1&from=USD&to=
代码入口:
GetInternetData getInternetData = new GetInternetData(); try { currencyMaps = getInternetData.getRequestInfo(); } catch (Exception e) { log.error(e.getMessage()); //return mapping.findForward("fail"); }
GetInternetData.java:
读取配置文件:
/** * 从网站读取数据 * @return * @throws Exception */ public final List<Exchangerate> getRequestInfo() throws Exception { //读取配置文件,取得网站路径 ResourceBundle bundle = ResourceBundle.getBundle("currencyURL"); Enumeration<String> enums = bundle.getKeys(); List<Exchangerate> currencyMaps = new ArrayList<Exchangerate>(); while (enums.hasMoreElements()) { String key = enums.nextElement(); //从网站中取得标签 if(!key.equals("YAHOO")){ log.info("==========>"+key); String res = getHttpRequestInfo(bundle.getString(key)); List<Exchangerate> currencyList = getData(res,key); currencyMaps.addAll(currencyList); } } return currencyMaps; }
由URL得到Response:
private final String getHttpRequestInfo(String uri) throws Exception { String response = null; // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Create a method instance. GetMethod method = new GetMethod(uri); // Provide custom retry handler is necessary HttpMethodParams httpMethodParams = method.getParams(); httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) throw new RuntimeException("Httpclient Method failed: " + method.getStatusLine()); response = method.getResponseBodyAsString(); String s1 = "<table"; String e1 = "</table>"; int stratIndex = response.indexOf(s1); int endIndex = response.indexOf(e1, stratIndex) + e1.length(); String currency = response.substring(stratIndex, endIndex); return currency; } finally { method.releaseConnection(); } }
组装数据:
private List<Exchangerate> getData(String response,String urlKey) throws TransformerException{ List<Exchangerate> currencyMaps = new ArrayList<Exchangerate>(); Document doc = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(response))); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 找到table的个数tr NodeList trNodes =doc.getDocumentElement().getElementsByTagName("tr"); log.info(trNodes.getLength()); for(int i=0;i<trNodes.getLength();i++){ Node node = trNodes.item(i); Node theadNode = XPathAPI.selectSingleNode(node, "thead"); if(null==theadNode){ NodeList tdNodes = XPathAPI.selectNodeList(node, "td"); Node tdnode = tdNodes.item(0); System.out.println("tdnodes: " + tdnode); if(null != tdnode) { //if ("CURRENCY".equalsIgnoreCase(tdNodes.item(0).getTextContent().trim())) continue; Exchangerate currency = new Exchangerate(); //将CURRENCY截取为BaseCurrency与TargetCurrency String[] cs = tdNodes.item(0).getTextContent().trim().split("[-]"); currency.setBasecurrency(cs[0]); currency.setSellcurrency(cs[1]); String amountStr = tdNodes.item(1).getTextContent().trim(); amountStr = amountStr.replaceAll(",", ""); currency.setWebExrate(Double.parseDouble(amountStr)); currency.setArea(urlKey); //currency.setUpdatedate(date); currencyMaps.add(currency); } } } return currencyMaps; }
。。。
方式二:
package tags; import java.io.BufferedReader; public class MainTest { /** * @param args */ public static void main(String[] args) { String fromCurrency = "HKD"; String toCurrency = "AUD"; String googleCur = getGoogleCurrency(fromCurrency, toCurrency); String yahooCur = getYahooCurrency(fromCurrency, toCurrency); System.out.println(googleCur+"\n"+yahooCur); } static String getGoogleCurrency(String fromCurrency, String toCurrency){ String rtn = ""; String strGoogleURL = "http://www.google.com/ig/calculator?hl=en&q=1" + fromCurrency + "%3D%3F" + toCurrency; try { // Create a URL for the desired page URL url = new URL(strGoogleURL); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { rtn += str; // str is one line of text; readLine() strips the newline character(s) } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return rtn; } static String getYahooCurrency(String fromCurrency, String toCurrency){ String rtn = ""; String strYahooURL = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=" + fromCurrency + toCurrency + "=X"; try { // Create a URL for the desired page URL url = new URL(strYahooURL); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { rtn += str; // str is one line of text; readLine() strips the newline character(s) } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return rtn; } }
。。。
相关推荐
我编程第一个webservice程序.首先iis配置好,然后project.dll文件拷贝iis目录。iis设置ip,然后路由器打开端口,开放自己计算机。客户端运行没有问题,我已经测试了。
一个webservice的创建与调用的例子C#
一个webService例子 动态调用,希望大家看看,给点意见
详细描述建立一个WebService工程的步骤,包括如果建立、如何部署、如何测试等方法。
第一个webservice例子.pdf第一个webservice例子.pdf
axis2插件是Myeclipse的一个webservice插件。
第一次做的WebService练习,希望有点帮助吧
这是利用webservice做的一个简单的hello world的程序 对于初学webservice的朋友来说是一个不可多得的好东西,由此可以举一反三,可以帮助你很快学习webservice
做智能客户端的时候,当用到WebService时,由于服务器处理速度、网络传输速度等各种原因会使一个WebService从请求开始到获得响应结果之间等待一段时间,这时候线程会处于阻塞状态,程序会等待请求结果导致客户端无法...
首先我们创建一个WebService,里面有两个方法:一个无参的方法,一个有参的方法: 创建好了WebService以后,把WebService部署到IIS上,并确保可以访问 二、静态引用 这种方式是通过添加静态引用的方式调用...
WebService接口说明public String getUserInfo(String username,String password)
网上写vb6.0调用webservice的都写得比较简单, 这个结合实例作了较详细介绍
客户端调用服务器WebService接口可以更加快速。
Windchill webservice集成方式的说明及使用文档、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
蛮简陋的一个项目,适合新手使用。 这是一个包含简单登录和查询的Web项目,内附有一个表sql文件和两个java项目。...WS_Client是客户端,是一个project项目,内部有一个WebService的测试类,用来测试从服务端取得数据
一个简单的WebService程序 便于新手操作
Web service 就是一个应用程序,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web来调用这个应用程序。我们把调用这个Web service 的应用程序叫做客户端。 Web service是一种跨...
我们在做sharepoint 2013的项目开发过程中,有时候需要用到webservice,webservice的好处就是方便其它应用系统直接或间接地操作sharepoint的列表数据。
利用javaws和cxf 编写的一个webService例子,可以作为WebService学习入门的例子
cxf+aegis发布webService配置简单、开发周期短,开发不用太过关心接口具体发布的过程以及接口描述文件wsdl的定义,这些工作都由cxf与aegis完成,如果一个项目中有需要集成接口或者项目比较紧张,采用cxf+aegis发布...