当前位置: 首页 > news >正文

获取网站的实时股票数据

以下代码可以解析WAP的PHP网页以及JSP网页;如果是要解析JSP网页,代码可以更加简略。
/**

 *本文来自http://blog.csdn.net/hellogv/

 * 这个单元负责全局函数

 */

import java.io.*;

import java.util.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

import javax.microedition.rms.*;

import java.lang.String;





public class cls_Stock {

    RecordStore rs=null;

    public cls_Stock() {

    }

    //-----------------------------------以下核心代码--------------------------------------------------------

    //从【股票信息】中返回指定的字符,从str_content中,提取开头为separator,结尾为str_end的之间的字符串

    public String GetSubStr(String str_content,String separator,String str_end)

    {

        int pos1=str_content.indexOf(separator)+separator.length();

        int pos2=0;

        if(str_end==null)

           pos2=str_content.length();     

        if(str_end!=null)

           pos2=str_content.indexOf(str_end, pos1);

        return str_content.substring(pos1, pos2);

    }

    //连接指定URL,取得股票信息,ConnectNet()控制ReturnStock()

    public String ConnectNet(String url,String separator,String end,String[] strs_filter)

    {    

        try{

            HttpConnection hc = (HttpConnection)Connector.open(url, Connector.READ_WRITE);

            hc.setRequestMethod(HttpConnection.POST);

            DataOutputStream dos = hc.openDataOutputStream();   

            DataInputStream dis = new DataInputStream(hc.openInputStream());

        //-------------------------关键代码:第一步获取整个网页的数据下载回来--------------------------------

            byte []str=new byte[2000];//从内存申请空间

            dis.read(str);//把读取返回的信息保存在str中  

            String content= XMLToString(str,strs_filter);//把str转换为字符串

        //-------------------------关键代码:第二步提取关键的数据--------------------------------  

            content=ReturnStock(content,separator,end,strs_filter);

           

            return content; 

        }catch(Exception e){return "出现错误!\n也许是网络连接错误、股票根本不存在或者现在股票休市!";}//出错则返回空字符

    }

   

      

    public String XMLToString(byte[] rec,String[] strs_filter) { //从字节读取内容        

           ByteArrayInputStream bais = new ByteArrayInputStream(rec);

           DataInputStream dis = new DataInputStream(bais);

           String BTS=null;

           try {

               BTS=new String(rec,"UTF-8");

               bais.close();

               dis.close();

           } catch (Exception e) {

               e.printStackTrace();

           }

           if (BTS.indexOf("")>0)//表示不能转换为汉字,则要过滤转换(提高效率)

           {

               for(int i=0;i<strs_filter.length;i++)//根据字段,循环把汉字替换为UTF码       

               {

                  BTS=replaceStr(BTS,GBtoUTF(strs_filter[i]),strs_filter[i]);

               }

           }

           return BTS;     

       }



    //从一堆XML代码中搜索有用的股票信息

    //根据strs_filter的元素作为查找字符串的开头

    //end作为结尾

    //separator作为分隔头和尾的标志

    public String ReturnStock(String content,String separator,String end,String[] strs_filter)

    {

        String str="",str_fieldname="";

        for(int i=0;i<strs_filter.length;i++)

        {

            int pos1=content.indexOf(strs_filter[i]);

            int pos2=content.indexOf(separator, pos1+1);

            int pos3=content.indexOf(end, pos2+1);

            str_fieldname=content.substring(pos1, pos1+strs_filter[i].length());

            str=str+"\n"+str_fieldname+content.substring(pos2, pos3);

        }    

        return str;

    }

//----------------------一下两个函数GBtoUTF,replaceStr配合一起使用---------------------------------

   //把汉字转化为UTF代码

    public static String GBtoUTF(String gb2312String) {

        if (gb2312String == null) {

            return null;

        }

        StringBuffer sb = new StringBuffer(gb2312String.length() * 8);



       int j = 0;

        for (int i = 0; i < gb2312String.length(); i++) {

            j = gb2312String.charAt(i);

            sb.append("");

            sb.append(Integer.toHexString(j).toLowerCase());

            sb.append(";");

        }

        return sb.toString();

    }



 //替换字符串函数

    public static String replaceStr(String str, String OldStr, String replace){

        for(int i=str.indexOf(OldStr); i>=0; i=str.indexOf(OldStr, i-1))

        {     

            if(i==0){                

                str = replace+str.substring(i+1, str.length());    

            }    

            else{        

                str = str.substring(0, i)+replace+str.substring(i+1, str.length());     

            }

        }

        return str;

    }

//----------------------以上两个函数GBtoUTF,replaceStr配合一起使用---------------------------------

   

     //-----------------------------------以上核心代码--------------------------------------------------------

 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python3 自定义函数无法被调用_Access查询中如何调用自定义函数
  • excel hutool 增加标题_Hutool Java 工具类库导出 Excel,超级简单!
  • 关于工作
  • mysql db host类型_MySQL的基本使用相关知识
  • mysql到底可不可以用join_到底可不可以使用join?
  • 在 Windows Mobile 上使用 GDI+
  • python class namedtuple 效率_Python数据结构:一个被低估的Namedtuple(一)
  • 在Tech Ed上要看到的Sync Service for ADO.NET
  • python具有可嵌入性_python基础第一节
  • 壮壮出世了
  • mysql数据库中本体数据_基于关系型数据库的本体持久化
  • 使用Ghost32.exe进行点对点计算机克隆
  • python正则通配符_通配符、正则表达式、python去重
  • 深入研究Windows内部原理系列(视频)(微软制造)
  • root命令连接mysql_通过更改root密码实现连接MySQL及常用命令
  • 08.Android之View事件问题
  • avalon2.2的VM生成过程
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • Facebook AccountKit 接入的坑点
  • js数组之filter
  • php ci框架整合银盛支付
  • React-生命周期杂记
  • Spring框架之我见(三)——IOC、AOP
  • Sublime Text 2/3 绑定Eclipse快捷键
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 给github项目添加CI badge
  • 关于extract.autodesk.io的一些说明
  • 警报:线上事故之CountDownLatch的威力
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 面试总结JavaScript篇
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 前端自动化解决方案
  • 如何使用 JavaScript 解析 URL
  • 手机端车牌号码键盘的vue组件
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 学习Vue.js的五个小例子
  • 用Visual Studio开发以太坊智能合约
  • ​香农与信息论三大定律
  • # wps必须要登录激活才能使用吗?
  • # 消息中间件 RocketMQ 高级功能和源码分析(七)
  • #include到底该写在哪
  • #Linux(Source Insight安装及工程建立)
  • #QT(串口助手-界面)
  • #微信小程序:微信小程序常见的配置传旨
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • $.ajax,axios,fetch三种ajax请求的区别
  • (2020)Java后端开发----(面试题和笔试题)
  • (70min)字节暑假实习二面(已挂)
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (poj1.3.2)1791(构造法模拟)
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • .gitignore文件—git忽略文件
  • .net Application的目录