调用天气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项目,手动迁移到小程序中使用.现在分享到互联网社区中,帮助后续有 ...
随机推荐
- 烂泥:Linux源码包制作RPM包之Apache
本文由秀依林枫提供友情赞助,首发于烂泥行天下 公司服务器比较多,需要把apache源码包制作成rpm包,然后放到公司内网yum源上进行下载安装.apache的rpm包安装方式比源码安装方式比较快,这能 ...
- Swift实现截屏并保存相册
func saveToLocal() { //截屏 let screenRect = UIScreen.mainScreen().bounds UIGraphicsBeginImageContext( ...
- main()函数的完整形式
初学C语言都觉得main作为整个程序的入口函数是不需要传递参数的,但事实上,我们完全可以给main()传入参数进而控制整个程序的执行,就像我们使用DOS命令传入的参数一样,这里面argc表示传入的参数 ...
- HQL的一些语句总结
HQL原文来自:http://slaytanic.blog.51cto.com/2057708/782175/ Slaytanic老师 关于Hadoop的介绍来自:http://www.cnblo ...
- vim IDE平台-打造属于自己的配置
vim IDE平台-打造属于自己的配置 一.前言 目前工作环境基本以Linux为主,自然用到VIM也很多,很早就对如何提高VIM的使用效率有所研究,限于时间关系,也没做个系统记录和资料积累,时间久了又 ...
- 在ASP.NET MVC中使用Unity进行依赖注入的三种方式
在ASP.NET MVC4中,为了在解开Controller和Model的耦合,我们通常需要在Controller激活系统中引入IoC,用于处理用户请求的 Controller,让Controller ...
- finereport普通报表的移动端自适应方案
移动端报表呈现,首先要求的是页面随手机屏幕大小自动放缩(自适应),下面给出一个普通报表中的finereport移动端自适应方案,适用于finereport 7.1之前的版本. 首先,了解一下当前我们可 ...
- css实现一个写信的格式
一.目标 目标实现如下效果: 二.完成 1.分析 这个效果看起来很简单,实际上可能并不那么容易实现. 首先是全部东西都居中显示,除了“亲爱的starof”这个称呼的地方.这也是难点,也是本文要重点说的 ...
- UART Explained(转载)
做嵌入式开发,UART几乎是必不可少的,调试串口.GPS.GPRS.Bluetooth等模块很多都是用的UART接口.时下火热的IoT也不乏UART的身影,串口的BLE.WIFI.Zigbee.Lor ...
- Android中一张图片加载后所占用内存大小的获取与测试
Android程序中一旦加载的图片比较多,就有可能出现Out of Memory而导致程序崩溃.这个一方面是因为Android系统本身对于每个单独的进程有内存大小的限制(有16M,64M,128M,2 ...