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. * Display information of one's selected funds class<br>
  25. * <br>
  26. * First step, synchronize the newest fund's value from http://www.peaklau.com/fund/index.php .<br>
  27. *   The request format is like this: http://www.peaklau.com/fund/index.php?C0=162703&C1=110002.<br>
  28. *   The response from http://www.peaklau.com/fund/index.php is in XML format.<br>
  29. * <I> <VERSION>1.0.7 </VERSION><br>
  30. * <F><br>
  31. *   <C>162703 </C><br>
  32. *   <N>广发小盘成长股票基金 </N><br>
  33. *   <D>2007-04-30 </D><br>
  34. *   <V>21570 </V><br>
  35. *   <P>11347 </P><br>
  36. * </F><br>
  37. * <F><br>
  38. *   <C>110002 </C><br>
  39. *   <N>xxx基金 </N><br>
  40. *   <D>2007-04-30 </D><br>
  41. *   <V>35940 </V><br>
  42. *   <P>29504 </P><br>
  43. * </F><br>
  44. * </I><br>
  45. *
  46. * <br>
  47. *
  48. * Second step, parse XML data and update local records.<br>
  49. *
  50. * <br>
  51. *
  52. * Finally, call setTxt() to show the data and the processing time.
  53. * <br>
  54. * @author peaklau <br>
  55. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  56. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  57. * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
  58. */
  59. public class ValueScreen extends ScrollScreen {
  60. public ValueScreen(String title,String txt,String backDisplayable,INavigator navigator){
  61. super(title,txt,backDisplayable,navigator);
  62. }
  63. protected void execute(){
  64. setTxt(DataCenter.getValueDesc());
  65. String requestString=DataCenter.getFundRequestString();
  66. if((requestString==null)||(requestString.length()==0)) return;
  67. setStatus("正在同步...");
  68. HttpClient httpClient=navigator.getHttpClient();
  69. try{
  70. httpClient.httpGet("http://"+host+"/fund/index.php?"+requestString);
  71. byte[]buf=httpClient.getData();
  72. int size=httpClient.getSize();
  73. String xml=new String(buf,0,size,"UTF-8");
  74. kXMLElement foo = new kXMLElement();
  75. foo.parseString(xml, 0);
  76. Enumeration listFundEnum = foo.enumerateChildren();
  77. while (listFundEnum.hasMoreElements()) {
  78. kXMLElement fund= (kXMLElement)(listFundEnum.nextElement());
  79. if(fund.getTagName().equals("VERSION")){
  80. navigator.setAppVersion(fund.getContents());
  81. continue;
  82. }
  83. Enumeration list= fund.enumerateChildren();
  84. String code="000000";
  85. String name="未知";
  86. String date="未知";
  87. int value=0;
  88. int percent=0;
  89. while (list.hasMoreElements()) {
  90. kXMLElement p= (kXMLElement)(list.nextElement());
  91. if(p.getTagName().equals("C")){
  92. code=p.getContents();
  93. }else if(p.getTagName().equals("N")){
  94. name=p.getContents();
  95. }else if(p.getTagName().equals("D")){
  96. date=p.getContents();
  97. }else if(p.getTagName().equals("V")){
  98. value=Integer.parseInt(p.getContents());
  99. }else if(p.getTagName().equals("P")){
  100. percent=Integer.parseInt(p.getContents());
  101. }
  102. }
  103. DataCenter.updateRecord(code,name,date,value,percent);
  104. }
  105. }catch(Exception e){
  106. setStatus("网络故障");
  107. e.printStackTrace();
  108. return;
  109. }
  110. setTxt(DataCenter.getValueDesc());
  111. setStatus("同步完成时间 "+getCurrentTime());
  112. }
  113. }