/* * * Params: yleProgram * Description: Create item list with an item prefab, then set its values. * */ private void CreateList(YLEProgram yleProgram) { if (yleProgram.data != null) { results.AddRange(yleProgram.data); Debug.Log("data is not null"); foreach (Datum program in yleProgram.data) { // Instantiate the item list GameObject item = GameObject.Instantiate(listItemPrefab); // Set the item list parent as 'programList' then the scale is fixed item.transform.SetParent(programList); item.transform.localScale = Vector3.one; // Pass 'Datum' object to ProgramDatum component that is used then to show the details item.GetComponentInChildren <ProgramDatum> ().program = program; // Setting the item title Text itemTxt = item.GetComponentInChildren <Text> (); if (program.itemTitle.fi != null) { itemTxt.text = program.itemTitle.fi; } else if (program.itemTitle.sv != null) { itemTxt.text = program.itemTitle.sv; } else if (program.itemTitle.en != null) { itemTxt.text = program.itemTitle.en; } if (itemTxt.text.Length > maxCharacters) { itemTxt.text = itemTxt.text.Substring(0, maxCharacters) + "..."; } } } else { Debug.Log("data is NULL"); } }
/* * * Coroutine that invokes a network call * to use the API and then populate * the program list * */ private IEnumerator GetPrograms() { Debug.Log("Searching Programs from YLE"); this.ShowLoading(); // Constructing the URI string apiUri = GetAPIUri(); Debug.Log("APIUri: " + apiUri); using (UnityWebRequest www = UnityWebRequest.Get(apiUri)) { searchingPrograms = true; yield return(www.Send()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { // Show results as text Debug.Log(www.downloadHandler.text); // YLEProgram is a model that deserialize the json values obtained by the API call YLEProgram response = JsonUtility.FromJson <YLEProgram> (www.downloadHandler.text); // Lets test the response Debug.Log("YLE Api Version: " + response.apiVersion); // Now create the program list this.CreateList(response); // Or retrieve results as binary data //byte[] results = www.downloadHandler.data; } searchingPrograms = false; } this.HideLoading(); }