调用天气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; }
}
}
调用天气Api实现天气查询的更多相关文章
- Java调用yahoo!API获取天气数据
先把代码复制上来,以后再做补充 package com.weather.test; import java.io.InputStream; import java.net.URL; import ja ...
- 免费天气API,天气JSON API,不限次数获取十五天的天气预报
紧急情况说明: 禁用IP列表: 39.104.69.*(原因39.104.69.6 在2018年10月的 17~20日 排行为top 1,每天几十万次.) 47.98.211.* (原因47.98.2 ...
- 使用小米天气API获取天气信息
1. URL部分 以下url中"%s"代表的是城市Id,比如北京的cityId=101010100: //获取未来五天预报信息,红色部分信息不需要 WEATHER_DATA_URL ...
- 获取新浪天气api显示天气情况(转)
直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> ...
- 高德地图API获取天气
1.建立行政区规划清单表 use edw; drop table if exists dim_prov_city_adcode; create table if not exists dim_prov ...
- JAVA的免费天气api接口调用示例
step1:选择本文所示例的接口"免费天气api" url:https://www.juhe.cn/docs/api/id/39/aid/87 step2:每个接口都需要传入一个参 ...
- 雅虎天气API调用
雅虎天气API调用: 1.调用方法:http://weather.yahooapis.com/forecastrss?w=2502265&u=c,绿色字体为城市代号,u=c表示取摄氏度. 2. ...
- ajax调用免费的天气API
最近在做项目中要用到调用天气接口,在网上找了很多资料之后发现https://www.tianqiapi.com/的天气API挺好的,好用而且免费,调用也很简单.在此做个笔记,大家一起学习交流,如有问题 ...
- 微信小程序-基于高德地图API实现天气组件(动态效果)
微信小程序-基于高德地图API实现天气组件(动态效果) 在社区翻腾了许久,没有找到合适的天气插件.迫不得已,只好借鉴互联网上的web项目,手动迁移到小程序中使用.现在分享到互联网社区中,帮助后续有 ...
随机推荐
- Ezchip Tilera Tile-Mx100: Der 100-ARM-Netzwerkprozessor
Ezchip Tilera Tile-Mx100: Der 100-ARM-Netzwerkprozessor ARM-Kerne statt VLIW-Einheiten: Tileras neue ...
- MMORPG大型游戏设计与开发(客户端架构 part4 of vegine)
昨天是七夕,祝大家都过的快乐,希望这句迟到的问候不会造成大家心中的困扰.这一节讲到了前端比较重要的模块,性能以及调试异常模块.一个应用的性能往往是最核心的部分,就像人身体的各个器官一样,一小部分也不能 ...
- Wish You to Remember
Just to myself: it is not complicate. And I don't know its internal principle now. (ms08-067) But I ...
- USB hacker Collection
This blog contains some ideas and tricks about USB hacking.
- vlan协议及端口类型
一.VLAN协议 1.协议的应用 802.1Q协议,即Virtual Bridged Local Area Networks协议,主要规定了VLAN的实现. 2.协议结构 每一个支持802.1Q协议的 ...
- Integer & int & == & equals
int 是基本类型,直接存数值,integer是对象,用一个引用指向这个对象 int 是基本数据类型,Integer是类 int类的变量初始为0,Integer的变量则初始化为null. 如果只是用来 ...
- 将pdf文件通过itunes直接拖到ipad的ibooks里面
开始不太清楚进行过什么设置,使得以前可以直接通过拖动的方式复制pdf文件到ipad里面的方法不管用了.在帖子http://bbs.weiphone.com/read-htm-tid-864091-pa ...
- CSS3文本超出容器显示省略号之text-overflow属性
text-overflow:ellipsis; overflow:hidden; white-space:nowrap; 要想实现文本超出容器时显示省略号,上面3个属性必须同时搭配使用
- js模拟高级语言的重载
js以递归的方式模拟高级语言的重载,我以添加元素节点为例子: //现有的子元素之前插入一个新的子元素 var before = function(elem,newElement,targetEleme ...
- Ant 命令行编译Android项目
首先把android sdk下的tools目录加到系统path环境变量里, 要么就得直接指定android.bat的绝对路径 对于一个新项目, 可以用这个命令创建需要的ant编译环境(可以看到andr ...