筆記

          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 閱讀(239) 評論(0)  編輯  收藏 所屬分類: work

          導航

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

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          收藏夾

          博客

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 闽侯县| 平湖市| 靖宇县| 汝州市| 嘉峪关市| 师宗县| 凤冈县| 怀宁县| 达州市| 长泰县| 新兴县| 尼勒克县| 福建省| 寻乌县| 蓬安县| 防城港市| 康乐县| 广宗县| 开原市| 涞源县| 宜兰市| 攀枝花市| 湘潭县| 六安市| 赞皇县| 广汉市| 浦江县| 易门县| 岱山县| 班玛县| 晋中市| 白河县| 太仓市| 清远市| 环江| 逊克县| 广宁县| 汽车| 宝清县| 西平县| 玛多县|