调用天气Api实现天气查询

调用天气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 = ; 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 = ; 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 = ; 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; }
}
}
上一篇:【python系列】python2.x和python3.x的区别


下一篇:(私人收藏)[开发必备]HTML5最全快速查找离线手册(可查询可学习,带实例)