Obtain real-time stock data of a website

Source: Internet
Author: User
The following code parses the wap php webpage and JSP webpage. If you want to parse the JSP webpage, the code can be simpler.
/*** From http://blog.csdn.net/hellogv/ * This Unit is responsible for the global function */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 () {}// ------------------------------------- the following core code ------------------------------------------- ------------- // Return the specified character from [Stock info]. From str_content, extract the 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);} // connect to the specified URL to obtain stock information. connectnet () controls 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 (); // ------------------------- key code: Step 1: download the data from the entire webpage -------------------------------- byte [] STR = new byte [2000]; // apply for DIS from the memory. read (STR); // Save the returned read information in STR string content = xmltostring (STR, strs_filter); // convert STR to string // ----------------------- key code: step 2 extract key data -------------------------------- content = returnstock (content, separator, end, strs_filter ); Return content;} catch (exception e) {return "error! /N may be a network connection error, the stock does not exist at all, or the stock is closed now! ";}// If an error occurs, an empty character is returned.} Public String xmltostring (byte [] rec, string [] strs_filter) {// read 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) // if it cannot be converted to Chinese characters, filter and convert (improve efficiency) {for (INT I = 0; I <strs_filter.length; I ++) // replace Chinese characters with UTF code {BTS = replacestr (BTS, gbtoutf (strs_filter [I]), strs_filter [I]) cyclically Based on fields;} return BTS ;} // search for useful stock information from a bunch of XML Code // use the strs_filter element as the start of the search string // end as the end // separator as the Public String sign to separate the header and tail 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;} // ---------------------- two functions: gbtoutf, replacestr works with --------------------------------- // convert Chinese characters to UTF code 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 () ;}// replace string function 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 ;}// ---------------------- use the above two functions gbtoutf and replacestr together --------------------------------- // compile the above core code example --------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.