调用天气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项目,手动迁移到小程序中使用.现在分享到互联网社区中,帮助后续有 ...
随机推荐
- ubuntu下apache2 安装 配置 卸载 CGI设置 SSL设置
一.安装.卸载apache2 apache2可直接用命令安装 sudo apt-get install apache2 卸载比较麻烦,必须卸干净,否则会影响ap ...
- (转)Yii的组件机制之一:组件基础类CComponent分析
Yii的组件机制 组件机制,是Yii整个体系的思想精髓,在使用Yii之前,最应该先了解其组件机制,如果不了解这个机制,那么阅读Yii源代码会非常吃力.组件机制给Yii框架赋予了无穷的灵活性和可扩展性, ...
- FastCV安装报错---LaunchAnyWhere错误:载入Java VM时Windows出现错误:2
- C语言中内存分配那些事儿
C程序的内存结构 C语言的之所以复杂,首先它的内存模型功不可没.不像某些那样的高级语言只需要在使用对象的时候,用new创建.所有之后的事情,你不需要操心.对于C语言,所有与内存相关的东西,都需要熟悉, ...
- 浅谈输入输出”重定向“——基于Linux系统
前言 进程在启动后会自动的打开3个文件:标准输入.标准输出和标准错误输出分别对应文件描述符0.1.2.对于每个进程他们都都维护了一张文件描述符表(file descriptor table),通常fd ...
- jquery常用方法
一.多个按钮绑定同一事件 $("#index_svip,#index_svip_renew").click(function() { seajs.use(['svipLayer'] ...
- css3中变形与动画(一)
css3制作动画的几个属性:变形(transform),过渡(transition)和动画(animation). 首先介绍transform变形. transform英文意思:改变,变形. css3 ...
- cuda多线程间通信
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <std ...
- AC日记——组合数问题 落谷 P2822 noip2016day2T1
题目描述 组合数表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的定 义,我们可以给出计算 ...
- [No00001A]天天换图,百词斩到底在折腾啥