阿里云天气预报接口代码
来源:互联网 发布:工控软件开发是什么 编辑:程序博客网 时间:2024/06/10 05:18
private const String path = "/weather/";
private const String method = "GET";
private const String appcode = "edc41a0bfae74864b90ce267502e029a";
String querys = "city=枣庄";
String bodys = "";
String url = host + path;
HttpWebRequest httpRequest = null;
HttpWebResponse httpResponse = null;
if (0 < querys.Length)
{
// url = url + "/city";
url = url + "query?" + querys;
}
if (host.Contains("https://"))
{
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
}
else
{
httpRequest = (HttpWebRequest)WebRequest.Create(url);
}
httpRequest.Method = method;
httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
if (0 < bodys.Length)
{
byte[] data = Encoding.UTF8.GetBytes(bodys);
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (WebException ex)
{
httpResponse = (HttpWebResponse)ex.Response;
}
string s1 = httpResponse.StatusCode.ToString() + httpResponse.StatusCode.ToString() + httpResponse.Method + httpResponse.Headers;
Stream st = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
string s2 = reader.ReadToEnd();
// Console.WriteLine(reader.ReadToEnd());
// Console.WriteLine("\n");
int d1 = s2.IndexOf("\"daily\":[");
int d2 = s2.IndexOf("\"hourly\":[");
string s3 = s2.Substring(d1 + 8, d2 - d1 - 9);
///
//s3 = "[{date:2017-03-21,week:星期二,sunrise:06:12,sunset:18:22,night:{weather:多云,templow:5,img:1,winddirect:无持续风向,windpower:微风},day:{weather:晴,temphigh:16,img:0,winddirect:无持续风向,windpower:微风}},{date:2017-03-22,week:星期三,sunrise:06:11,sunset:18:23,night:{weather:小雨,templow:4,img:7,winddirect:无持续风向,windpower:微风},day:{weather:多云,temphigh:11,img:1,winddirect:无持续风向,windpower:微风}},{date:2017-03-23,week:星期四,sunrise:06:09,sunset:18:24,night:{weather:小雨,templow:6,img:7,winddirect:无持续风向,windpower:微风},day:{weather:小雨,temphigh:11,img:7,winddirect:无持续风向,windpower:微风}},{date:2017-03-24,week:星期五,sunrise:06:08,sunset:18:25,night:{weather:多云,templow:5,img:1,winddirect:无持续风向,windpower:微风},day:{weather:多云,temphigh:12,img:1,winddirect:无持续风向,windpower:微风}},{date:2017-03-25,week:星期六,sunrise:06:07,sunset:18:26,night:{weather:多云,templow:3,img:1,winddirect:无持续风向,windpower:微风},day:{weather:多云,temphigh:16,img:1,winddirect:无持续风向,windpower:微风}},{date:2017-03-26,week:星期日,sunrise:07:30,sunset:19:30,night:{weather:晴,templow:3,img:0,winddirect:西北风,windpower:4级},day:{weather:多云,temphigh:16,img:1,winddirect:西北风,windpower:4级}},{date:2017-03-27,week:星期一,sunrise:07:30,sunset:19:30,night:{weather:晴,templow:4,img:0,winddirect:西北风,windpower:微风},day:{weather:晴,temphigh:18,img:0,winddirect:西北风,windpower:微风}}]";
s3 = s3.Replace("\"", "");
dt1.Columns.Add("date");
dt1.Columns.Add("week");
dt1.Columns.Add("weat");
dt1.Columns.Add("low");//templow
dt1.Columns.Add("high");//temphigh
dt1.Columns.Add("RiQi");//temphigh
while (true)
{
DataRow dr1 = dt1.NewRow();
d1 = s3.IndexOf("date");
if (d1 > -1)
{
dr1[0] = s3.Substring(d1 + 5, 10);
dr1[0] = dr1[0].ToString().Replace(",", "");
}
else
{
break;
}
d1 = s3.IndexOf("week");
if (d1 > -1)
{
dr1[1] = s3.Substring(d1 + 5, 3);
dr1[1] = dr1[1].ToString().Replace(",", "");
}
d1 = s3.IndexOf("weather");
if (d1 > -1)
{
string s4 = s3.Substring(d1 + 8, 10);
d1 = s4.IndexOf(",");
if (d1 > -1)
{
s4 = s4.Substring(0, d1);
}
dr1[2] = s4;
dr1[2] = dr1[2].ToString().Replace(",", "");
}
d1 = s3.IndexOf("templow");
if (d1 > -1)
{
dr1[3] = s3.Substring(d1 + 8, 2);
dr1[3] = dr1[3].ToString().Replace(",", "");
}
d1 = s3.IndexOf("temphigh");
if (d1 > -1)
{
dr1[4] = s3.Substring(d1 + 9, 2);
dr1[4] = dr1[4].ToString().Replace(",", "");
}
dt1.Rows.Add(dr1);
s3 = s3.Substring(d1 + 10);
}
dt1.Columns.Add("t");
for (int i = 0; i < dt1.Rows.Count; i++)
{
DateTime dtime2 = DateTime.Parse(dt1.Rows[i]["date"].ToString());
dt1.Rows[i]["RiQi"] = dtime2.ToString("yyyy年MM月dd日");
}
for (int i = 0; i < dt1.Rows.Count; i++)
{
for (int j = 0; j < dt0.Rows.Count; j++)
{
if (dt1.Rows[i]["date"].ToString().Trim() == dt0.Rows[j]["date"].ToString().Trim())
{
dt1.Rows[i]["t"] = "1";
break;
}
}
}
具体内容参看:
- 阿里云天气预报接口代码
- Android 端天气预报APP的实现(二)阿里云天气预报API的获取
- 阿里云天气api调用方法。
- 配置Blog今气预报!
- Android自定义View--用贝塞尔曲线实现一加多云天气
- 阿里云天池医疗大赛:用AI攻克全球癌症难题
- 【观察】得生态者得云天下 阿里、腾讯、浪潮、华为对决云计算2.0
- 城市天气三小时预报,天气预报接口实现
- 气
- 气
- 漫步云天
- 今儿心情不太好,多敲几行代码消消气~
- 阿里云天池工业AI大赛暨中国“印象盐城,数创未来”大数据竞赛正式启动
- 阿里云天池医疗AI大赛第一季迎来最后决赛,医疗AI面临哪些机遇与阻碍
- 早间预报
- 阿里接口链接
- 阿里大于短信接口
- 阿里大鱼短信接口:
- jQuery radio的取值与赋值
- JVM源码分析之Java对象的创建过程
- 2016年度总结
- 使用AJAX时 设置ajax全局变量
- Java漂流记之SpringMVC(一)--序言
- 阿里云天气预报接口代码
- Android非mtk平台T9的实现
- STM32F407 串口编程USART1,USART2,USART3,UART4
- 找回Centos7的Root密码
- Spring学习笔记2017.03.22
- IOUtils--IO流应用
- S2013中如何解决error C4996: 'fopen'问题
- SSM之整合Redis
- linux-IO和c语言IO