基于C#的全国天气查询API调用代码实例

本文提供了一个使用C#调用全国天气查询API的实际代码示例,通过示例详细介绍了如何接入并获取全国各地区的天气信息。
摘要由CSDN通过智能技术生成

全国天气查询API:https://www.juhe.cn/docs/api/id/39

 

基于C#的全国天气查询API调用代码实例

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Xfrog.Net;
using System.Diagnostics;
using System.Web;
 
//----------------------------------
// 全国天气预报调用示例代码 - 聚合数据
// 在线接口文档:http://www.juhe.cn/docs/39
// 代码中JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439
//----------------------------------
 
namespace ConsoleAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            string appkey = "*******************"; //配置您申请的appkey
 
             
            //1.根据城市名/id查询天气
            string url1 = "http://v.juhe.cn/weather/index";
 
            var parameters1 = new Dictionary<string, string>();
 
            parameters1.Add("cityname" , ""); //城市名或城市ID,如:&quot;苏州&quot;,需要utf8 urlencode
            parameters1.Add("dtype" , ""); //返回数据格式:json或xml,默认json
            parameters1.Add("format" , ""); //未来6天预报(future)两种返回格式,1或2,默认1
            parameters1.Add("key", appkey);//你申请的key
 
            string result1 = sendPost(url1, parameters1, "get");
 
            JsonObject newObj1 = new JsonObject(result1);
            String errorCode1 = newObj1["error_code"].Value;
 
            if (errorCode1 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj1);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj1["error_code"].Value+":"+newObj1["reason"].Value);
            }
 
 
            //2.天气种类及标识列表
            string url2 = "http://v.juhe.cn/weather/uni";
 
            var parameters2 = new Dictionary<string, string>();
 
            parameters2.Add("key", appkey);//你申请的key
            parameters2.Add("dtype" , ""); //返回数据的格式,xml或json,默认json
 
            string result2 = sendPost(url2, parameters2, "get");
 
            JsonObject newObj2 = new JsonObject(result2);
            String errorCode2
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值