java调天气预报

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java调天气预报相关的知识,希望对你有一定的参考价值。

http://www.weather.com.cn/data/sk/101010100.html 我把这个地址放在一个方法里想在运行这个方法是这个的时候这个地址去请求服务器怎么弄?

①取出地址中的返回值(getWeatherReader方法)
②解析json格式的字符串
③形成你要展示的天气预报效果
public static String getWeatherReader() //取得接口字符串
String currentLine = "";
String strReturn = "";
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
BufferedReader buff = null;
try
url = new URL("http://www.weather.com.cn/data/sk/101010100.html");
System.out.println(url.toURI());
//打开地址链接
conn = (HttpURLConnection)url.openConnection();
conn.connect();
//接收数据
in = conn.getInputStream();
//如有乱码注意编码方式,如:UTF-8
buff = new BufferedReader(new InputStreamReader(in, "gb2312"));
while((currentLine = buff.readLine()) != null)
strReturn += currentLine;

catch (Exception e)
e.printStackTrace();
finally
try
in.close();
buff.close();
catch (IOException e)
return "8EF0000";


return strReturn;
参考技术A 汗 我前天写了一个 也是调的中国天气网的数据 并且我还写了个数据采集器 把全国的城市代码采集下来了 放到一个XML文件

java web service实现天气预报功能

请问在java web service(有XFile 支持) 中要实现天气预报功能,怎么做?

前台js界面代码:

//省份
        function LoadProvince() 
            $.ajax(
                type: "POST",
                url: "ashx/weatherHandler.ashx",
                data: "option=province",
                success: function (result) 
                    $(".sel-province option").remove();
                    var arry = result.split(\'|\');
                    var obj = null;
                    for (var i = 0; i < arry.length; i++) 
                        if (arry[i] != null && arry[i] != "") 
                            obj = arry[i].split(\',\');
                            $(".sel-province").append("<option value=\'" + obj[1] + "\'>" + obj[0] + "</option>");
                        
                    
                    $(".sel-province").find("option[text=\'北京\']").attr("selected", "selected");
                ,
                error: function (errorMsg) 
                    $(".result-table tr").remove();
                    $(".result-table").append("<tr><td>省份请求出现错误,请您稍后重试。。。</td></tr>");
                
            );
        
        //城市
        function LoadCity(provinceid)    
            $.ajax(
                type: "POST",
                url: "ashx/weatherHandler.ashx",
                data: "provinceid=" + provinceid + "&option=city",
                success: function (result) 
                    $(".sel-city option").remove();
                    var arry = result.split(\'|\');
                    var obj = null;
                    for (var i = 0; i < arry.length; i++) 
                        if (arry[i] != null && arry[i] != "") 
                            obj = arry[i].split(\',\');
                            $(".sel-city").append("<option value=\'" + obj[1] + "\'>" + obj[0] + "</option>");
                        
                    
                ,
                error: function (errorMsg) 
                    $(".result-table tr").remove();
                    $(".result-table").append("<tr><td>城市请求出现错误,请您稍后重试。。。</td></tr>");
                
            );
        
        //加载天气
        function GetWeather(cityid) 
            $.ajax(
                type: "POST",
                url: "ashx/weatherHandler.ashx",
                data: "cityid=" + cityid + "&option=weather",
                success: function (result) 
                    $(".result-table tr").remove();
                    var arry = result.split(\'|\');
                    var obj = null;
                    for (var i = 0; i < arry.length; i++) 
                        if (arry[i] != null && arry[i] != "") 
                            if (arry[i].indexOf(".gif") > 0) 
                                $(".result-table").append("<tr><td><image src=\'images/" + arry[i] + "\'/></td></tr>");
                            
                            else 
                                $(".result-table").append("<tr><td>" + arry[i] + "</td></tr>");
                            
                        
                    
                ,
                error: function (errorMsg) 
                    $(".result-table tr").remove();
                    $(".result-table").append("<tr><td>天气数据请求出现错误,请您稍后重试。。。</td></tr>");
                
            );
        

html代码:

<body>
    <form id="form1" runat="server">
    <div class="head-div">
        <table>
            <tr>
                <td>
                    <select class="sel-province sel">
                    </select>
                </td>
                <td>
                    <select class="sel-city sel">
                    </select>
                </td>
                <td>
                    <input type="button" class="btn-search" value="查询" />
                </td>
            </tr>
        </table>
    </div>
    <div class="result-div">
        <table class="result-table">
        </table>
    </div>
    </form>
</body>

由于js不支持跨域,直接ajax+ashx一般处理程序(在里面调用天气接口)。一般处理程序代码如下:

using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
 
namespace WeatherTest.ashx

    /// <summary>
    /// weatherHandler 的摘要说明
    /// </summary>
    public class weatherHandler : IHttpHandler
    
        WeatherWsClient.WeatherWSSoapClient client = new WeatherWsClient.WeatherWSSoapClient();
        public void ProcessRequest(HttpContext context)
        
            context.Response.ContentType = "text/plain";
            string[] result = null;
            string option = context.Request.Form["option"];
            switch (option)
            
                case "province":
                    result = GetProvinces();
                    break;
                case "city":
                    result = GetCitys(context.Request.Form["provinceid"]);
                    break;
                case "weather":
                    result = GetWeather(context.Request.Form["cityid"], null);
                    break;
            
            string str = ConvertToString(result, option);
 
            context.Response.Write(str);
        
        /// <summary>
        /// 数组转字符串
        /// </summary>
        /// <param name="result"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        private string ConvertToString(string[] result, string option)
        
            StringBuilder sb = new StringBuilder();
            foreach (string item in result)
            
                sb.Append(item+"|");
            
            return sb.ToString();
        
 
        /// <summary>
        /// 省份
        /// </summary>
        /// <returns></returns>
        private string[] GetProvinces()
        
            return client.getRegionProvince();
        
        /// <summary>
        /// 城市
        /// </summary>
        /// <param name="provinceid"></param>
        /// <returns></returns>
        private string[] GetCitys(string provinceid)
        
            return client.getSupportCityString(provinceid);
        
        /// <summary>
        /// 天气数据
        /// </summary>
        /// <param name="cityid"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        private string[] GetWeather(string cityid, string userid)
        
            return client.getWeather(cityid, userid);
        
 
        public bool IsReusable
        
            get
            
                return false;
            
        
    
参考技术A 引用一个天气预报页就完事了不用自己写 我给你个网站你就知道了 http://www.265.com/weather/db.htm#54337天气预报的免费代码在页面最下面

以上是关于java调天气预报的主要内容,如果未能解决你的问题,请参考以下文章

如何用java远程访问天气预报接口

java web service实现天气预报功能

获取天气预报java代码

JAVA问题[天气]

Java学习随笔--爬虫--天气预报

公众号推送早安问候以及天气预报(JAVA)