上面是简单截图:

前台代码:

@{
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实现天气查询的更多相关文章

  1. Java调用yahoo!API获取天气数据

    先把代码复制上来,以后再做补充 package com.weather.test; import java.io.InputStream; import java.net.URL; import ja ...

  2. 免费天气API,天气JSON API,不限次数获取十五天的天气预报

    紧急情况说明: 禁用IP列表: 39.104.69.*(原因39.104.69.6 在2018年10月的 17~20日 排行为top 1,每天几十万次.) 47.98.211.* (原因47.98.2 ...

  3. 使用小米天气API获取天气信息

    1. URL部分 以下url中"%s"代表的是城市Id,比如北京的cityId=101010100: //获取未来五天预报信息,红色部分信息不需要 WEATHER_DATA_URL ...

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

    直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> ...

  5. 高德地图API获取天气

    1.建立行政区规划清单表 use edw; drop table if exists dim_prov_city_adcode; create table if not exists dim_prov ...

  6. JAVA的免费天气api接口调用示例

    step1:选择本文所示例的接口"免费天气api" url:https://www.juhe.cn/docs/api/id/39/aid/87 step2:每个接口都需要传入一个参 ...

  7. 雅虎天气API调用

    雅虎天气API调用: 1.调用方法:http://weather.yahooapis.com/forecastrss?w=2502265&u=c,绿色字体为城市代号,u=c表示取摄氏度. 2. ...

  8. ajax调用免费的天气API

    最近在做项目中要用到调用天气接口,在网上找了很多资料之后发现https://www.tianqiapi.com/的天气API挺好的,好用而且免费,调用也很简单.在此做个笔记,大家一起学习交流,如有问题 ...

  9. 微信小程序-基于高德地图API实现天气组件(动态效果)

    微信小程序-基于高德地图API实现天气组件(动态效果) ​ 在社区翻腾了许久,没有找到合适的天气插件.迫不得已,只好借鉴互联网上的web项目,手动迁移到小程序中使用.现在分享到互联网社区中,帮助后续有 ...

随机推荐

  1. sql server 2005导出数据到oracle

    一. 在sql server下处理需要导出的数据库 1. 执行以下sql,查出所有'float'类型的字段名,手动将float类型改为decimal(18,4). select 表名=d.name,字 ...

  2. 查询数据过多页面反应慢引入缓存解决方案(Redis、H2)

      问题:原系统查询接口不支持分页也不可能加入分页支持,导致Ajax查询数据过多,返回数据达到2W多条记录时响应已经极慢,查询功能不要求数据实时性,页面反应速度极慢.体验不好:经排查是由于数据量过大导 ...

  3. Android 解读.apk解压后文件详细说明

    转自:http://xdang.org/post-602.html 以下原文: 反编译 — 在apk文件中能得到什么 最近在做android客户端与服务器安全通信,有一种常见的不安全因素:很多软件常常 ...

  4. MySQL事务学习-->隔离级别

    MySQL事务学习-->隔离级别 6 事务的隔离级别 设置的目的 在数据库操作中,为了有效保证并发读取数据的正确性,提出的事务隔离级别. 数据库是要被广大客户所共享访问的,那么在数据库操作过程中 ...

  5. 学习OpenStack之 (3):Devstack Screen 使用技巧

    Devstack环境中,openstack运行在一个screen中,每个service运行在一个window中.我总结的几个tips: 0. 注意需要使用screen启动用户来进行一下操作 1. 查看 ...

  6. 还是不想改报告,伊阿忆啊哟-Linux基础继续

    hi 虽然今天是最最美好的周六(前不着工作日后不着工作日),但老子还要来改报告,但额就是不想改,你拿我有啥办法啊... 争取完结Linux基础 一.Linux常用命令(三) 4.帮助命令 4.1 帮助 ...

  7. 翻译《Writing Idiomatic Python》(三):变量、字符串、列表

    原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...

  8. Win 2003硬盘安全设置

    C:分区部分: c: administrators 全部 iis_wpg 只有该文件夹 列出文件夹/读数据 读属性 读扩展属性 读取权限 c:inetpubmailroot administrator ...

  9. jQuery实例

    1.$("ul li").fliter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')").addClas ...

  10. nfs服务部署记录

    一.概念介绍NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系统能 ...