Why isn't my code with a restful api in it npt working
My gps code works and I connected the text and button correctly but I don't get anything on the text about the temperature. And I can't figure out to solve the problem. And yes I tried out the links without the code and it gives me a json file.
Here is the link to the api documentation: https://openweathermap.org/current
Here is my code that categorises the json file:
[System.Serializable]
public class WeatherInformation
{
public string lon;
public string lat;
public string description; //what kind of weather
public string temp;
public string feels_like; //how temperature feels
public string all; //clouds
public string name;
}
Here is my code that asks the api and categorises the json with the code that categorises it and returns it for other code to use it and the API key are Xses now because otherwise people can steal it
using UnityEngine;
using System.Net;
using System.IO;
using UnityEngine.Networking;
public static class APIHelper
{
public static WeatherInformation GetNewWeatherInformation()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.openweathermap.org/data/2.5/weather?lat=" + GPS.Instance.latitude.ToString().Replace(",", ".") + "&lon=" + GPS.Instance.longitude.ToString().Replace(",", ".") + "&units=metric&appid=" + "XXXXXXXXXX&units=metric");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
return JsonUtility.FromJson<WeatherInformation>(json);
}
}
This is the code that updates it by pressing the button and when the app starts also updates it using UnityEngine; using TMPro;
public class Weather : MonoBehaviour
{
public TextMeshProUGUI tempOut;
public TextMeshProUGUI cityOut;
public void Start()
{
WeatherInformation w = APIHelper.GetNewWeatherInformation();
tempOut.text = w.temp + "°C";
cityOut.text = w.name;
}
// hooked up to a button
public void UpdateWeather()
{
WeatherInformation w = APIHelper.GetNewWeatherInformation();
tempOut.text = w.temp + "°C";
cityOut.text = w.name;
}
}
AND YES THE TEXT IS CONNECTED TO THE SCRIPT!
The app is meant for android but I also tried it just on windows but both didn't work