分享

获取网站的实时股票数据

 长江黄鹤 2013-12-29
Java代码  收藏代码
  1. /** 
  2.  
  3.  *本文来自http://blog.csdn.net/hellogv/ 
  4.  
  5.  * 这个单元负责全局函数 
  6.  
  7.  */  
  8.   
  9. import java.io.*;  
  10.   
  11. import java.util.*;  
  12.   
  13. import javax.microedition.midlet.*;  
  14.   
  15. import javax.microedition.lcdui.*;  
  16.   
  17. import javax.microedition.io.*;  
  18.   
  19. import javax.microedition.rms.*;  
  20.   
  21. import java.lang.String;  
  22.   
  23.   
  24.   
  25.   
  26.   
  27. public class cls_Stock {  
  28.   
  29.     RecordStore rs=null;  
  30.   
  31.     public cls_Stock() {  
  32.   
  33.     }  
  34.   
  35.     //-----------------------------------以下核心代码--------------------------------------------------------  
  36.   
  37.     //从【股票信息】中返回指定的字符,从str_content中,提取开头为separator,结尾为str_end的之间的字符串  
  38.   
  39.     public String GetSubStr(String str_content,String separator,String str_end)  
  40.   
  41.     {  
  42.   
  43.         int pos1=str_content.indexOf(separator)+separator.length();  
  44.   
  45.         int pos2=0;  
  46.   
  47.         if(str_end==null)  
  48.   
  49.            pos2=str_content.length();       
  50.   
  51.         if(str_end!=null)  
  52.   
  53.            pos2=str_content.indexOf(str_end, pos1);  
  54.   
  55.         return str_content.substring(pos1, pos2);  
  56.   
  57.     }  
  58.   
  59.     //连接指定URL,取得股票信息,ConnectNet()控制ReturnStock()  
  60.   
  61.     public String ConnectNet(String url,String separator,String end,String[] strs_filter)  
  62.   
  63.     {      
  64.   
  65.         try{  
  66.   
  67.             HttpConnection hc = (HttpConnection)Connector.open(url, Connector.READ_WRITE);  
  68.   
  69.             hc.setRequestMethod(HttpConnection.POST);  
  70.   
  71.             DataOutputStream dos = hc.openDataOutputStream();     
  72.   
  73.             DataInputStream dis = new DataInputStream(hc.openInputStream());  
  74.   
  75.         //-------------------------关键代码:第一步获取整个网页的数据下载回来--------------------------------  
  76.   
  77.             byte []str=new byte[2000];//从内存申请空间  
  78.   
  79.             dis.read(str);//把读取返回的信息保存在str中    
  80.   
  81.             String content= XMLToString(str,strs_filter);//把str转换为字符串  
  82.   
  83.         //-------------------------关键代码:第二步提取关键的数据--------------------------------    
  84.   
  85.             content=ReturnStock(content,separator,end,strs_filter);  
  86.   
  87.              
  88.   
  89.             return content;   
  90.   
  91.         }catch(Exception e){return "出现错误!\n也许是网络连接错误、股票根本不存在或者现在股票休市!";}//出错则返回空字符  
  92.   
  93.     }  
  94.   
  95.      
  96.   
  97.         
  98.   
  99.     public String XMLToString(byte[] rec,String[] strs_filter) { //从字节读取内容          
  100.   
  101.            ByteArrayInputStream bais = new ByteArrayInputStream(rec);  
  102.   
  103.            DataInputStream dis = new DataInputStream(bais);  
  104.   
  105.            String BTS=null;  
  106.   
  107.            try {  
  108.   
  109.                BTS=new String(rec,"UTF-8");  
  110.   
  111.                bais.close();  
  112.   
  113.                dis.close();  
  114.   
  115.            } catch (Exception e) {  
  116.   
  117.                e.printStackTrace();  
  118.   
  119.            }  
  120.   
  121.            if (BTS.indexOf("")>0)//表示不能转换为汉字,则要过滤转换(提高效率)  
  122.   
  123.            {  
  124.   
  125.                for(int i=0;i<strs_filter.length;i++)//根据字段,循环把汉字替换为UTF码         
  126.   
  127.                {  
  128.   
  129.                   BTS=replaceStr(BTS,GBtoUTF(strs_filter[i]),strs_filter[i]);  
  130.   
  131.                }  
  132.   
  133.            }  
  134.   
  135.            return BTS;       
  136.   
  137.        }  
  138.   
  139.   
  140.   
  141.     //从一堆XML代码中搜索有用的股票信息  
  142.   
  143.     //根据strs_filter的元素作为查找字符串的开头  
  144.   
  145.     //end作为结尾  
  146.   
  147.     //separator作为分隔头和尾的标志  
  148.   
  149.     public String ReturnStock(String content,String separator,String end,String[] strs_filter)  
  150.   
  151.     {  
  152.   
  153.         String str="",str_fieldname="";  
  154.   
  155.         for(int i=0;i<strs_filter.length;i++)  
  156.   
  157.         {  
  158.   
  159.             int pos1=content.indexOf(strs_filter[i]);  
  160.   
  161.             int pos2=content.indexOf(separator, pos1+1);  
  162.   
  163.             int pos3=content.indexOf(end, pos2+1);  
  164.   
  165.             str_fieldname=content.substring(pos1, pos1+strs_filter[i].length());  
  166.   
  167.             str=str+"\n"+str_fieldname+content.substring(pos2, pos3);  
  168.   
  169.         }      
  170.   
  171.         return str;  
  172.   
  173.     }  
  174.   
  175. //----------------------一下两个函数GBtoUTF,replaceStr配合一起使用---------------------------------  
  176.   
  177.    //把汉字转化为UTF代码  
  178.   
  179.     public static String GBtoUTF(String gb2312String) {  
  180.   
  181.         if (gb2312String == null) {  
  182.   
  183.             return null;  
  184.   
  185.         }  
  186.   
  187.         StringBuffer sb = new StringBuffer(gb2312String.length() * 8);  
  188.   
  189.   
  190.   
  191.        int j = 0;  
  192.   
  193.         for (int i = 0; i < gb2312String.length(); i++) {  
  194.   
  195.             j = gb2312String.charAt(i);  
  196.   
  197.             sb.append("");  
  198.   
  199.             sb.append(Integer.toHexString(j).toLowerCase());  
  200.   
  201.             sb.append(";");  
  202.   
  203.         }  
  204.   
  205.         return sb.toString();  
  206.   
  207.     }  
  208.   
  209.   
  210.   
  211.  //替换字符串函数  
  212.   
  213.     public static String replaceStr(String str, String OldStr, String replace){  
  214.   
  215.         for(int i=str.indexOf(OldStr); i>=0; i=str.indexOf(OldStr, i-1))  
  216.   
  217.         {       
  218.   
  219.             if(i==0){                  
  220.   
  221.                 str = replace+str.substring(i+1, str.length());      
  222.   
  223.             }      
  224.   
  225.             else{          
  226.   
  227.                 str = str.substring(0, i)+replace+str.substring(i+1, str.length());       
  228.   
  229.             }  
  230.   
  231.         }  
  232.   
  233.         return str;  
  234.   
  235.     }  
  236.   
  237. //----------------------以上两个函数GBtoUTF,replaceStr配合一起使用---------------------------------  
  238.   
  239.      
  240.   
  241.      //-----------------------------------以上核心代码--------------------------------------------------------  
  242.   
  243.    

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约