1. /*
  2. * Copyright 1999,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Created on 2007-2-14
  18. *
  19. */
  20. package peaklau.eaglefund;
  21. import java.util.Enumeration;
  22. import nanoxml.kXMLElement;
  23. /**
  24. * Get the newest ZhongZheng100 and HuShen300 index value <br>
  25. *
  26. * <br>
  27. * The request is the standard HTTP request, and the URL is http://www.peaklau.com/fund/stock.php.<br>
  28. * The response is in XML format.<br>
  29. * eg:<br>
  30. * <I><br>
  31. *   <S><N>沪深300</N><br>
  32. *   <L><D>05-08</D><V>368600</V></L><br>
  33. *   ...<br>
  34. *   </S><br>
  35. *   <S><N>中证100</N><br>
  36. *   <L><D>05-08</D><V>362900</V></L><br>
  37. *   ...<br>
  38. *   </S><br>
  39. * </I><br>
  40. *
  41. * <br>
  42. * @author peaklau <br>
  43. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  44. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  45. * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
  46. */
  47. public class StockScreen extends ScrollScreen {
  48. private static int LIST_COUNT=30;
  49. public StockScreen(String title,String txt,String backDisplayable,INavigator navigator){
  50. super(title,txt,backDisplayable,navigator);
  51. }
  52. protected void execute(){
  53. setStatus("正在同步...");
  54. StringBuffer sb=new StringBuffer();
  55. HttpClient httpClient=navigator.getHttpClient();
  56. try{
  57. httpClient.httpGet("http://"+host+"/fund/stock.php");
  58. byte[]buf=httpClient.getData();
  59. int size=httpClient.getSize();
  60. String xml=new String(buf,0,size,"UTF-8");
  61. kXMLElement foo = new kXMLElement();
  62. foo.parseString(xml, 0);
  63. //System.out.println("<" + foo.getTagName() + ">");
  64. Enumeration listIndexEnum = foo.enumerateChildren();
  65. while (listIndexEnum.hasMoreElements()) {
  66. kXMLElement index= (kXMLElement)(listIndexEnum.nextElement());
  67. Enumeration indexList= index.enumerateChildren();
  68. String name="";
  69. String []time=new String[LIST_COUNT];
  70. long []value=new long[LIST_COUNT];
  71. long []percent=new long[LIST_COUNT];
  72. int count=0;
  73. while (indexList.hasMoreElements()) {
  74. kXMLElement p= (kXMLElement)(indexList.nextElement());
  75. if(p.getTagName().equals("N")){
  76. name=p.getContents();
  77. }else if(p.getTagName().equals("L")){
  78. Enumeration attrs= p.enumerateChildren();
  79. while (attrs.hasMoreElements()&&(count<LIST_COUNT)){
  80. kXMLElement attr= (kXMLElement)(attrs.nextElement());
  81. if(attr.getTagName().equals("D")){
  82. time[count]=attr.getContents();
  83. }else if(attr.getTagName().equals("V")){
  84. value[count]=Integer.parseInt(attr.getContents());
  85. }
  86. }
  87. count++;
  88. }
  89. }
  90. for(int i=0;i<LIST_COUNT-1;i++){
  91. if(value[i+1]!=0){
  92. percent[i]=(value[i]-value[i+1])*10000Lvalue[i+1];
  93. //System.out.println(value[i]+" "+value[i+1]+" "+percent[i]);
  94. }
  95. }
  96. sb.append(" \r\n"+name+"\r\n");
  97. for(int i=0;i<LIST_COUNT-1;i++){
  98. if(percent[i]>=0){
  99. sb.append("<00ff0000> "+time[i]+" "+(value[i]/100)+" "+(percent[i]/100)+"."+DataCenter.getFormatInteger(percent[i]%100,2)+"%\r\n");
  100. }else{
  101. percent[i]=0-percent[i];
  102. sb.append("<0000ff00> "+time[i]+" "+(value[i]/100)+" -"+(percent[i]/100)+"."+DataCenter.getFormatInteger(percent[i]%100,2)+"%\r\n");
  103. }
  104. }
  105. sb.append("\r\n\r\n");
  106. }
  107. setTxt(sb.toString());
  108. }catch(Exception e){
  109. setStatus("网络故障");
  110. e.printStackTrace();
  111. return;
  112. }
  113. setStatus("同步完成时间 "+getCurrentTime());
  114. }
  115. }