调用天气Api实现天气查询

上面是简单截图:

前台代码:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
        #dvShow{
            width:800px;
            margin:0px auto;
            background-color:#F5FAFE;
        }
    </style>
</head>
<body>
    <input type="button" value="点击查看天气情况" id="btn1"/>
    <div id="dvShow">

    </div>
</body>
</html>
<script src="~/Content/jquery.min.js"></script>
<script>
    $(function () {
        $("#btn1").click(function () {
        $.getJSON(‘@Url.Action("Index2", "Home")‘,null,function(_data){
            var recwhether = _data.whether;
            var recindexdata = _data.indexdata;
            for (var i = 0; i < recwhether.length; i++) {
                $("<p>" + recwhether[i].date + "</p><p><img src=" + recwhether[i].dayPictureUrl + "/></p><p><img src=" + recwhether[i].nightPictureUrl + "/></p><p>" + recwhether[i].weather + "</p><p>" + recwhether[i].wind + "</p><p>" + recwhether[i].temperature + "</p><p>" + recindexdata[i].title + "</p><p>" + recindexdata[i].zs + "</p><p>" + recindexdata[i].tipt + "</p><p>" + recindexdata[i].des + "</p><hr/>").appendTo("#dvShow");
            }
        });

        });
    });
</script>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Xml;
using System.Xml.Linq;
using WebAPITest.Models;

namespace WebAPITest.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Index2()
        {

            //获取接口数据
            HttpClient client = new HttpClient();

            string result = client.GetAsync("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=xml&ak=A72e372de05e63c8740b2622d0ed8ab1").Result.Content.ReadAsStringAsync().Result;
            //写入XML文档
            XmlDocument dom = new XmlDocument();
            dom.LoadXml(result);
            dom.Save(HttpContext.Request.MapPath("/Files/XML/show.xml"));
            //读取XML文档            

             XDocument XDoc2 = XDocument.Load(HttpContext.Request.MapPath("/Files/XML/show.xml"));
             //获取根节点
             XElement Root = XDoc2.Root;
             XElement xresults=Root.Element("results");
             XElement xcurrentCity=xresults.Element("currentCity");
             string cityStr = xcurrentCity.Value;
             //输出根节点的Name,Value
             string str1= Root.Name.ToString();//输出:北京
             XElement xweather_data=xresults.Element("weather_data");
            IEnumerable<XElement> xdate = xweather_data.Elements("date");
            List<string> dateList = new List<string>();
            foreach (var item in xdate) //获得data集合
            {
                dateList.Add(item.Value);
            }
            IEnumerable<XElement> xdayPictureUrl = xweather_data.Elements("dayPictureUrl");
            List<string> dayPictureUrlList = new List<string>();//获得dayPictureUrl集合
            foreach (var item in xdayPictureUrl)
            {
                dayPictureUrlList.Add(item.Value);
            }
             IEnumerable<XElement> xnightPictureUrl = xweather_data.Elements("nightPictureUrl");
             List<string> nightPictureUrlList = new List<string>();//获得nightPictureUrl集合
             foreach (var item in xnightPictureUrl)
             {
                 nightPictureUrlList.Add(item.Value);
             }
             List<string> weatherList = new List<string>();//获得weather集合
             IEnumerable<XElement> xweather = xweather_data.Elements("weather");
             foreach (var item in xweather)
             {
                 weatherList.Add(item.Value);
             }
             List<string> windList = new List<string>();//获得wind集合
             IEnumerable<XElement> xwind = xweather_data.Elements("wind");
             foreach (var item in xwind)
             {
                 windList.Add(item.Value);
             }
             List<string> temperatureList = new List<string>();//获得temperature集合
            IEnumerable<XElement> xtemperature = xweather_data.Elements("temperature");
            foreach (var item in xtemperature)
            {
                temperatureList.Add(item.Value);
            }
            List<BaiDuWheter> whteherDataList = new List<BaiDuWheter>();
            for (int i = 0; i < dateList.Count; i++)//将所有天气遍历追加到一起
            {
                BaiDuWheter bw = new BaiDuWheter();
                bw.date = dateList[i];
                bw.dayPictureUrl = dayPictureUrlList[i];
                bw.nightPictureUrl = nightPictureUrlList[i];
                bw.weather = weatherList[i];
                bw.wind = windList[i];
                bw.temperature = temperatureList[i];
                whteherDataList.Add(bw);
            }
            XElement index = xresults.Element("index");
            IEnumerable<XElement> xtitle=index.Elements("title");
            List<string> titleList = new List<string>();
            foreach (var item in xtitle)
            {
                titleList.Add(item.Value);
            }
            IEnumerable<XElement> xzs = index.Elements("zs");
            List<string> zsList = new List<string>();
            foreach (var item in xzs)
            {
                zsList.Add(item.Value);
            }
            IEnumerable<XElement> xtipt = index.Elements("tipt");
            List<string> tiptList = new List<string>();
            foreach (var item in xtipt)
            {
                tiptList.Add(item.Value);
            }
            IEnumerable<XElement> xdes = index.Elements("des");
            List<string> desList = new List<string>();
            foreach (var item in xdes)
            {
                desList.Add(item.Value);
            }
            List<IndexData> indexList = new List<IndexData>();
            for (int i = 0; i < titleList.Count; i++)//将所有的穿衣信息遍历整合到一起
            {
                IndexData data = new IndexData();
                data.title = titleList[i];
                data.zs = zsList[i];
                data.tipt = tiptList[i];
                data.des = desList[i];
                indexList.Add(data);
            }
            XElement pm25= xresults.Element("pm25");
            string pm25Str = pm25.Value;//计算书PM2.5的值
            XElement nowdata= Root.Element("date");
            string nowDataStr = nowdata.Value;//计算出现在的日期
            var sendData = new { whether=whteherDataList,indexdata=indexList};
            return Json(sendData, JsonRequestBehavior.AllowGet);

        }
    }
}

Model类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebAPITest.Models
{
    public class BaiDuWheter
    {
        public string date { get; set; }
        public string dayPictureUrl { get; set; }
        public string nightPictureUrl { get; set; }
        public string weather { get; set; }
        public string wind { get; set; }
        public string temperature { get; set; }
    }
    public class IndexData
    {
        public string title { get; set; }
        public string zs { get; set; }
        public string tipt { get; set; }
        public string des { get; set; }
    }
}

时间: 2024-08-06 11:58:27

调用天气Api实现天气查询的相关文章

获取新浪天气api显示天气情况(转)

直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>天气</title> </h

调用天气api实现查询各城市天气

调用的api数据为haoservice.com网站提供的天气数据. 如下图,我们需要向其传递的参数有两个,一个为我们自己申请的key,一个为城市名字. 首先定义两个变量,一个存储key,一个存储接口地址. public static String key = "674a9e7feb0d439d84e2dee75d964c**"; //key稍微变动了一下public static String queryUrl = "http://apis.haoservice.com/we

使用python调用和风天气API查询当前天气信息

和风天气网址:https://www.heweather.com/ 注册后在控制台会有个人认证key API帮助文档:https://www.heweather.com/documents/api/v5 #!/usr/bin/python #-*-coding:utf-8-*- #调用和风天气API查询当前天气信息 #2017/07/12 import json import urllib2 import os city='北京' #在和风天气注册后获得的key key='your key' c

JAVA调用聚合天气api接口示例

查询天气预报在APP中常用的一个常用功能,聚合数据免费天气api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查询. 代码描述:基于JAVA的免费天气api接口调用示例,根据文档中注明的需求参数,调用接口返回数据. 关联数据:免费天气api 接口地址:https://www.juhe.cn/docs/api/id/39 step1:选择本文所示例的接口"免费天气api" url:https://www.

wp8.1 调用中国天气api

在调用api应用的过程,我们需要用hmac加密技术,它是一种基于hash的加密算法,通过一个双方共同约定的密钥,在发送message前,对密钥进行了sha散列计算,在生成消息又对此密钥进行了二次加密,通过周期性的更换密钥,安全性可以得到保障. 在wp8.1 sdk中很多传统系统类库被整编进以windows打头的命名空间中,很多刚接触wp8.1朋友可能觉得疑惑. 代码所需命名空间. 1 using System; 2 using System.Net.Http; 3 using System.Th

天气API接口大全(nohacks.cn 收集整理)

自序: 由nohacks.cn 收集整理,来源于网络,版权归原作者所有,基本收集了网络上能使用的大部分天气API接口,作者水平精力有限,难免有遗漏或错误的地方,欢迎反馈,作者网站:http://nohacks.cn 更新记录: 2015.3.2    更新云聚,增加天气网接口. 2015.2.28  增加百度天气图片,感谢网友彬子的反馈. 2015. 2.19 增加百度天气接口,更新中国天气网API. 目录: 1.  中国天气网 1.1  实时天气 API 接口(失效) 1.2  当天天气 AP

天气预报接口api(中国天气网)

中国天气weather.comhttp://m.weather.com.cn/data/101110101.html(六天预报) http://www.weather.com.cn/data/sk/101110101.html(实时天气信息) 其中101110101是城市的代码,获得城市代码进入 http://www.weather.com.cn在搜索框上输入你要需要获得天气的城市,点击查询,即可在地址栏获得相应城市编号,然后替换http://m.weather.com.cn/data/1011

百度天气API接口

接口说明 根据经纬度/城市名查询天气的结果. 接口参数说明 参数类型 参数名称 是否必须 具体描述 String ak true 开发者密钥 String sn false 若用户所用ak的校验方式为sn校验时该参数必须.  String location true 输入城市名或经纬度,城市名称如:北京,经纬度格式为lng,lat坐标如: location=116.305145,39.982368;城市天气预报中间"|"分隔,location=116.305145,39.982368|

JSON之三:获取JSON文本并解释(以google的天气API为例)

google提供了天气的api,以广州天气为例,地址为: http://api.openweathermap.org/data/2.5/weather?q=guangzhou 返回的结果为: { "coord": { "lon": 113.25, "lat": 23.12 }, "sys": { "message": 0.2088, "country": "CN",