以下代码可以解析WAP的PHP网页以及JSP网页;如果是要解析JSP网页,代码可以更加简略。
/**
*本文来自http://blog.youkuaiyun.com/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配合一起使用---------------------------------
//-----------------------------------以上核心代码--------------------------------------------------------