- /*
- * Copyright 1999,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /*
- * Created on 2007-2-14
- *
- */
- package peaklau.eaglefund;
- import java.util.Enumeration;
- import nanoxml.kXMLElement;
- /**
- * Get the newest ZhongZheng100 and HuShen300 index value <br>
- *
- * <br>
- * The request is the standard HTTP request, and the URL is http://www.peaklau.com/fund/stock.php.<br>
- * The response is in XML format.<br>
- * eg:<br>
- * <I><br>
- * <S><N>沪深300</N><br>
- * <L><D>05-08</D><V>368600</V></L><br>
- * ...<br>
- * </S><br>
- * <S><N>中证100</N><br>
- * <L><D>05-08</D><V>362900</V></L><br>
- * ...<br>
- * </S><br>
- * </I><br>
- *
- * <br>
- * @author peaklau <br>
- * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
- * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
- * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
- */
- public class StockScreen extends ScrollScreen {
- private static int LIST_COUNT=30;
- public StockScreen(String title,String txt,String backDisplayable,INavigator navigator){
- super(title,txt,backDisplayable,navigator);
- }
- protected void execute(){
- setStatus("正在同步...");
- StringBuffer sb=new StringBuffer();
- HttpClient httpClient=navigator.getHttpClient();
- try{
- httpClient.httpGet("http://"+host+"/fund/stock.php");
- byte[]buf=httpClient.getData();
- int size=httpClient.getSize();
- String xml=new String(buf,0,size,"UTF-8");
- kXMLElement foo = new kXMLElement();
- foo.parseString(xml, 0);
- //System.out.println("<" + foo.getTagName() + ">");
- Enumeration listIndexEnum = foo.enumerateChildren();
- while (listIndexEnum.hasMoreElements()) {
- kXMLElement index= (kXMLElement)(listIndexEnum.nextElement());
- Enumeration indexList= index.enumerateChildren();
- String name="";
- String []time=new String[LIST_COUNT];
- long []value=new long[LIST_COUNT];
- long []percent=new long[LIST_COUNT];
- int count=0;
- while (indexList.hasMoreElements()) {
- kXMLElement p= (kXMLElement)(indexList.nextElement());
- if(p.getTagName().equals("N")){
- name=p.getContents();
- }else if(p.getTagName().equals("L")){
- Enumeration attrs= p.enumerateChildren();
- while (attrs.hasMoreElements()&&(count<LIST_COUNT)){
- kXMLElement attr= (kXMLElement)(attrs.nextElement());
- if(attr.getTagName().equals("D")){
- time[count]=attr.getContents();
- }else if(attr.getTagName().equals("V")){
- value[count]=Integer.parseInt(attr.getContents());
- }
- }
- count++;
- }
- }
- for(int i=0;i<LIST_COUNT-1;i++){
- if(value[i+1]!=0){
- percent[i]=(value[i]-value[i+1])*10000Lvalue[i+1];
- //System.out.println(value[i]+" "+value[i+1]+" "+percent[i]);
- }
- }
- sb.append(" \r\n"+name+"\r\n");
- for(int i=0;i<LIST_COUNT-1;i++){
- if(percent[i]>=0){
- sb.append("<00ff0000> "+time[i]+" "+(value[i]/100)+" "+(percent[i]/100)+"."+DataCenter.getFormatInteger(percent[i]%100,2)+"%\r\n");
- }else{
- percent[i]=0-percent[i];
- sb.append("<0000ff00> "+time[i]+" "+(value[i]/100)+" -"+(percent[i]/100)+"."+DataCenter.getFormatInteger(percent[i]%100,2)+"%\r\n");
- }
- }
- sb.append("\r\n\r\n");
- }
- setTxt(sb.toString());
- }catch(Exception e){
- setStatus("网络故障");
- e.printStackTrace();
- return;
- }
- setStatus("同步完成时间 "+getCurrentTime());
- }
- }