using LitJson; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using YHElectric; public class RealWeather : MonoBehaviour { string key; string city; string url; public delegate void SetWeather(string weather); public static event SetWeather setWeather; public bool getReal = false; void Start() { LoadKey(); } void LoadKey() { try { //string text = System.IO.File.ReadAllText(Application.streamingAssetsPath + "/WeatherKey.json"); //JsonData jd = JsonMapper.ToObject(text); //key = jd["key"].ToString(); //city = jd["city"].ToString(); //url = "http://v.juhe.cn/weather/index?format=2&cityname=" + city + "&key=" + key;//0beaedc48f4d6b2bcaea822cfa9e22f0 url = "http://v.juhe.cn/weather/index?format=2&cityname=" + "青岛" + "&key=" + "0beaedc48f4d6b2bcaea822cfa9e22f0";//0beaedc48f4d6b2bcaea822cfa9e22f0 print(url); if(getReal) StartCoroutine(SendRequest(url)); }catch(Exception e) { Debug.Log(e.ToString()); } } /// /// 开启一个协程,发送请求 /// /// IEnumerator SendRequest(string url) { Application.ExternalCall("DebugLogUnity", url); UnityWebRequest re = UnityWebRequest.Get(url); re.timeout = 5; yield return re.SendWebRequest(); //等待返回请求的信息 if (re.isHttpError || re.isNetworkError) //如果其 请求失败,或是 网络错误 { Debug.LogError(re.error); //打印错误原因 Application.ExternalCall("DebugLogUnity", re.error); } else //请求成功 { Debug.Log("请求成功"); string JsonData = re.downloadHandler.text; JsonData jd = JsonMapper.ToObject(JsonData); Debug.Log(re.downloadHandler.text); IndexPanel.tempTxt.text = jd["result"]["sk"]["temp"].ToString(); switch (jd["result"]["today"]["weather"].ToString()) { case "晴": break; case "多云": break; case "小雨": break; case "中雨": break; case "大雨": break; case "阴": break; } Application.ExternalCall("DebugLogUnity", jd["result"]["today"]["weather"].ToString()); setWeather(jd["result"]["today"]["weather"].ToString()); print(jd["result"]["today"]["weather"].ToString()); } } }