欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity天气信息获取

程序员文章站 2022-04-03 09:00:05
...

从网站获取信息格式为XMl

 static string weatherInfoUrl= "http://wthrcdn.etouch.cn/WeatherApi?citykey=" + 101270605;




    static string weatherstr = GetHtml(weatherInfoUrl);
  //  resp tempInfo = XmlDeSeralizer<resp>(weatherstr);

    List<string> strlist = xmlRead.xml_Read(weatherstr);

    private void Start()
    {
        foreach (var item in strlist)
        {
            print(item);
        }
    }


    private static string GetHtml(string url)
    {
       
        StringBuilder s = new StringBuilder(102400);
        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
        wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
        HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
        Head(response);
        GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
        byte[] d = new byte[20480];
        int l = g.Read(d, 0, 20480);
        while (l > 0)
        {
            s.Append(Encoding.UTF8.GetString(d, 0, l));
            l = g.Read(d, 0, 20480);
        }
        return s.ToString();
    }

    private static void Head(HttpWebResponse r)
    {
        string[] keys = r.Headers.AllKeys;
        for (int i = 0; i < keys.Length; ++i)
        {
            Console.WriteLine(keys[i] + "   " + r.Headers[keys[i]]);
        }
    }

    public static T XmlDeSeralizer<T>(string xmlStr) where T : class, new()
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        using (StringReader reader = new StringReader(xmlStr))
        {
            return xs.Deserialize(reader) as T;
        }
    }

解析得到的XML格式,将数据转换到一个字符串数组中

 public class xmlRead
    {
        
        public static List<string> xml_Read(string s)
        {
            XmlDocument document=new XmlDocument();
            document.LoadXml(s);

        List<string> str=new List<string>();

        string riqi = document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("date").InnerText;
        string gaowen =document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("high").InnerText;
        string diwen= document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("low").InnerText;
        string yun = document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("day").SelectSingleNode("type").InnerText;
        string feng = document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("day").SelectSingleNode("fengxiang").InnerText;
        string fengli = document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("day").SelectSingleNode("fengli").InnerText;
        string nightyu = document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("night").SelectSingleNode("type").InnerText;
        string nightfeng = document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("night").SelectSingleNode("fengxiang").InnerText;
        string nightfengli = document.SelectSingleNode("resp").SelectSingleNode("forecast").SelectSingleNode("weather").SelectSingleNode("night").SelectSingleNode("fengli").InnerText;

        str.Add(riqi);
        str.Add(gaowen);
        str.Add(diwen);
        str.Add(yun);
        str.Add(feng);
        str.Add(fengli);
        str.Add(nightyu);
        str.Add(nightfeng);
        str.Add(nightfengli);

        return str;
    }
}

 

相关标签: window Web