笔记

way

简单google weather调用(jsp实现)

weather.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.net.*"%>
<%
    StringBuffer sbf 
= new StringBuffer();
    
//Access the page
    try {
      
//如果网络设置了代理
        System.setProperty("http.proxyHost""xxx");
        System.setProperty(
"http.proxyPort""80");
        URL url 
= new URL("http://www.google.com/ig/api?weather=london");
        URLConnection urlConn 
= url.openConnection();

        BufferedReader in 
= new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        String inputLine;
        
while ((inputLine = in.readLine()) != null)
            sbf.append(inputLine);
        in.close();
        System.out.println(
"last="+sbf.toString());
    } 
catch (MalformedURLException e) {
        System.out.println(
"MalformedURLException"+e);
    } 
catch (IOException e) {
        System.out.println(
"IOException"+e);
    }
%><%=sbf.toString()%>
前台js部分:

        var childData   = function(selector, arg)
        {
                
return selector.find(arg).attr('data');
         }
        $.ajax({
            type : 
"GET",
            data : 
"where=" ,
            url : 
"weather.jsp",
            success : 
function(data) {
                console.debug('data
='+data);
                forecast 
= $(data).find('forecast_information');
                cCondition 
= $(data).find('current_conditions');

                city 
= childData(forecast, 'city');
                
if (city != undefined) {
                    date 
= childData(forecast, 'forecast_date');

                    condition 
= childData(cCondition, 'condition');
                    tempC 
= childData(cCondition, 'temp_c');
                    humidity 
= childData(cCondition, 'humidity');
                    icon 
= childData(cCondition, 'icon');
                    $('#city').text(city);
                    $('#date').text(date);
                    $('#condition').text(condition);
                    $('#tempC').html(tempC 
+ '&deg; C');
                    $('#humidity').text(humidity);
                    $('#icon').attr({
                        'src' : 'http:
//www.google.com' + icon
                    });
                    $('#data').stop().show('fast');
                } 
else {
                    $('#error').stop().show('fast');
                }
            }
        });

posted on 2012-02-22 08:58 yuxh 阅读(227) 评论(0)  编辑  收藏 所属分类: work


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

统计

常用链接

留言簿

随笔分类

随笔档案

收藏夹

博客

搜索

最新评论

阅读排行榜

评论排行榜