The weather forecast realizes the small plan

Source: Internet
Author: User

1, iframe Other people's labor results ...

Benefits: Quick

Cons: Not all service providers support IFRAME, they can't modify the style

such as: QQ weather

<iframe src= "http://weather.news.qq.com/inc/ss295.htm" style= "WIDTH:212PX; height:204px "border=0 marginwidth=0 marginheight=0 frameborder=no width=160 scrolling=no height=60></iframe >

China Weather Net http://www.weather.com.cn

<iframesrc= "http://www.weather.com.cn/static/custom/custom.html" height= "193" scrolling= "no" frameborder= "0" ></iframe>

2. How to implement Java

The use of Technology httpclient crawl page implementation, in fact, is quite simple, but according to lazy too strong, if someone else's website has been modified, also must make corresponding changes. High Maintenance costs

HttpClient HttpClient = new HttpClient ();

Httpclient.gethostconfiguration (). SetProxy ("Agent Host", 80); Because I'm using an agent locally.

String Hosturl = "http://www.weather.com.cn/static/custom/custom.html";//Fetch address

GetMethod GetMethod = new getmethod (Hosturl);

Using the system-provided default recovery policy

Getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler, new Defaulthttpmethodretryhandler ()) ;

Try {

Executive GetMethod

int statusCode = Httpclient.executemethod (GetMethod);

if (StatusCode! = HTTPSTATUS.SC_OK) {

System.err.println ("Method failed:" + getmethod.getstatusline ());

}

InputStream Resstream = Getmethod.getresponsebodyasstream (); File is large. So the build justice uses the flow mode

BufferedReader br = new bufferedreader (new InputStreamReader (Resstream));

StringBuffer Resbuffer = new stringbuffer ();

String restemp = "";

while ((Restemp = Br.readline ()) = null) {

Resbuffer.append (restemp);

}

Br.close ();

String response = resbuffer.tostring (); Crawled remote files have been organized into strings

3, Ajax+java Realization Way

jquery+dom4j

The implementation method is relatively flexible and is recommended in this way. Because Ajax has a cross-domain limitation problem, a Java class is used to make a proxy, to help communicate with the remote server, and then give the return information to the AJAX processing

Ajax.js

$.ajax ({

Type: "POST",

URL: "dataresource.jsp?city=" +$ ("#city"). Val (),

DataType: "xml",//here is puzzled, need to return the XML format, write the error ...

Success:function(result) {

Do something

},

Error:function(msg) {alert ("E")}

})

dataresource.jsp//Preferably write this function in a separate class

String City = Request.getparameter ("city");

URL url = null;

String urlstr =null;

if (city!=null)

Urlstr = "http://www.google.com/ig/api?hl=zh_cn&weather=" +city;

Else

Urlstr = "http://www.google.com/ig/api?hl=zh_cn&weather=beijing";

url = new URL (urlstr);

Properties prop = System.getproperties ();

Prop.put ("Http.proxyhost", "proxy host");

Prop.put ("Http.proxyport", "80");

InputStream in = Url.openstream ();

InputStreamReader ISR =new inputstreamreader (in, "GBK");

Saxreader reader = new saxreader ();

Document doc = Reader.read (ISR);;

Response.getwriter (). Write (Doc.asxml ());//returns the XML format

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.