Hello!
I'm writing code that goes out to an API and gets data in JSON, I have created classes to use the JSON utility to decode this JSON. Therefore(As far as I'm aware) I need to use a method instead of a coroutine to return the data that I get from the JSON decode. So in order to make the method wait for the UnityWebRequest to finish, I was using a while method. I found out that it will make the WebGL freeze, So I've been trying to find a way to make it wait without using a coroutine.
**Here's My Code: **
public TaskList FindTasks()
{
const string URL = "https://api.harvestapp.com/v2/tasks";
using (UnityWebRequest www = UnityWebRequest.Get(URL))
{
www.SetRequestHeader("user-agent", "MyApp (max@benmiller.com)");
www.SetRequestHeader("Authorization", "Bearer " + AccessToken);
www.SetRequestHeader("Harvest-Account-Id", AccountID);
www.Send();
while (!www.isDone)
{
}
new WaitUntil(www.isDone);
if (www.isHttpError)
{
Debug.LogError("UnityWebError: " + www.error);
return null;
}
else
{
string Value = www.downloadHandler.text;
TaskList JsonData = JsonUtility.FromJson(Value);
return JsonData;
}
}
}
[Serializable]
public class TaskList
{
public List tasks;
}
[Serializable]
public class Task
{
public int id;
public string name;
public bool billable_by_default;
public string default_hourly_rate; // No clue what type this should be.
public bool is_default;
public bool is_active;
public DateTime created_at;
public DateTime updated_at;
}
↧