以下代码可以解析WAP的PHP网页以及JSP网页;如果是要解析JSP网页,代码可以更加简略。
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.
责任编辑:小草