Ventuz 获取实时天气信息

本教程教你在Script中如何获取天气信息

1:新建一个Script Node 双击进入脚本编辑器
a.添加Inputs变量

在这里插入图片描述

b.添加Outputs变量

在这里插入图片描述

c.代码覆盖
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Ventuz.Kernel;

using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;

/// <summary>
/// 会自动执行一次更新
/// 修改CityName或者调用ReGetData方法就可以更新数据
/// </summary>
public class Script : ScriptBase, System.IDisposable
{
	public class HTTPS
	{
		public delegate void AsyncRequestHandler(string data, string url, System.Action<string> callback);
		public static void AsyncRequest(string data, string url, System.Action<string> callback) {
			AsyncRequestHandler ar = AsyncRequestMethod;
			ar.BeginInvoke(data, url, callback, null, null);
		}
		private static void AsyncRequestMethod(string data, string url, System.Action<string> callback)
		{
			string result = Request(data, url);
			if (callback != null)
			{
				callback(result);
			}
		}
		public static string Request(string data, string url)
		{
			return Request(Encoding.GetEncoding("UTF-8").GetBytes(data), url);
		}

		public static string Request(byte[] data, string url)
		{
			string result = string.Empty;

			//创建一个客户端的Http请求实例
			HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
			request.ContentType = "application/x-www-form-urlencoded";
			request.Method = "POST";
			request.ContentLength = data.Length;
			Stream requestStream = request.GetRequestStream();
			requestStream.Write(data, 0, data.Length);
			requestStream.Close();

			//获取当前Http请求的响应实例
			HttpWebResponse response = request.GetResponse() as HttpWebResponse;
			Stream responseStream = response.GetResponseStream();
			using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
			{
				result = reader.ReadToEnd();
			}
			responseStream.Close();

			return result;
		}
	}

	private const string url = "https://www.tianqiapi.com/api/?version=v1&city=";
	private string latestCityName = "";
	private bool isNeedInitData = true;
	// This member is used by the Validate() method to indicate
	// whether the Generate() method should return true or false
	// during its next execution.
	private bool changed;
    
	// This Method is called if the component is loaded/created.
	public Script()
	{ 
		// Note: Accessing input or output properties from this method
		// will have no effect as they have not been allocated yet.
	}
    
	// This Method is called if the component is unloaded/disposed
	public virtual void Dispose()
	{
	}
    
	// This Method is called if an input property has changed its value
	public override void Validate()
	{
		// Remember: set changed to true if any of the output 
		// properties has been changed, see Generate()
		if ((string.IsNullOrEmpty(this.CityName)==false && latestCityName.Equals(this.CityName) == false))
		{
			latestCityName = this.CityName;
			OnReGetData(-1);
		}	
		else if (isNeedInitData == true)
		{
			OnReGetData(-1);
		}
	}
    
	
	// This Method is called every time before a frame is rendered.
	// Return value: if true, Ventuz will notify all nodes bound to this
	//               script node that one of the script's outputs has a
	//               new value and they therefore need to validate. For
	//               performance reasons, only return true if output
	//               values really have been changed.
	public override bool Generate()
	{
		if (changed)
		{
			changed = false;
			return true;
		}

		return false;
	}

	//重新从服务器获取数据
	// This Method is called if the function/method ReGetData is invoked by the user or a bound event.
	// Return true, if this component has to be revalidated!
	public bool OnReGetData(int arg)
	{
		if (string.IsNullOrEmpty(this.CityName)==true)
		{
			return false;
		}
		isNeedInitData = false;
		HTTPS.AsyncRequest("", url+this.CityName, (reusult) => {
			this.httpResult = reusult;
			this.changed = true;
			});
		return false;
	}

}
2:新建JsonParser Node 解析 获取到的天气数据

在这里插入图片描述
3:效果图
在这里插入图片描述

4:里面有其他数据字段可根据自己需求享用
5: 有问题可以找我:zyf151@qq.com
6:示例vza(v6.3):
	链~接:https://pan.baidu.com/s/1pMwneUpw5f_mopCn_mWdDg
	提~取~码:gl5g
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牙膏上的小苏打2333

哟,哟,切克闹,煎饼果子来一套

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值