Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- using System.IO;
- using SimpleJSON;
- [System.Serializable]
- public class JSONManager : Manager
- {
- // API Url
- private const string URL = "https://external.api.yle.fi/v1/programs/items.json?";
- //limit of the loaded items
- private const int limit = 10;
- //Search bar
- [SerializeField]
- private Text search;
- //Api data (API key and Api ID)
- [SerializeField]
- private APIData APIInfo;
- //startindex
- [SerializeField]
- private int offset = 0;
- public int Offset
- {
- get
- {
- return offset;
- }
- set
- {
- offset = value;
- }
- }
- private UIManager uiManager
- {
- get
- {
- return GameManager.Instance.GetManager<UIManager>();
- }
- }
- /// <summary>
- /// This function execute when the user click on the search
- /// </summary>
- public void StartRequest()
- {
- GameManager.Instance.GetManager<JSONManager>().Offset = 0;
- GameManager.Instance.StartCoroutine(RequestAPI());
- }
- /// <summary>
- /// Creating the URL.
- /// </summary>
- /// <param name="query">Searching query</param>
- private string CreateUrl(string query, int limit,int offset)
- {
- Debug.Log(offset);
- return URL + "q=" + query + "&offset=" + offset + "&limit=" + limit +"&app_id=" + APIInfo.AppID + "&app_key=" + APIInfo.AppKey;
- }
- /// <summary>
- /// Requestng the Api
- /// </summary>
- /// <returns></returns>
- private IEnumerator RequestAPI()
- {
- //Clear the UI Items
- uiManager.ClearItems();
- //Requesting API
- UnityWebRequest Requesting = UnityWebRequest.Get(CreateUrl(search.text,limit, offset));
- yield return Requesting.Send();
- //check if the request doesnt have errors
- if (!Requesting.isNetworkError || !Requesting.isHttpError || !Requesting.isNetworkError || Requesting != null)
- {
- //Parse the UnityWebRequest to an object.
- JSONNode node = JSONArray.Parse(Requesting.downloadHandler.text).AsObject;
- //checking if the node is find
- if (node == null)
- Debug.Log("NULL");
- //list of 10 items
- List<YleItem> items = new List<YleItem>();
- for (int i = 0; i < node[2].Count; i++)
- {
- //getting the title and description.
- string title = node["data"][i]["title"]["fi"];
- string description = node["data"][i]["description"]["fi"];
- //adding it and making it a object
- items.Add(new YleItem(title, description));
- }
- //Updating the items.
- for (int i = 0; i < node[2].Count; i++)
- uiManager.UpdateItems(items);
- }
- else
- {
- Debug.LogError("Error");
- }
- }
- }
- /// <summary>
- /// all the API data
- /// </summary>
- [System.Serializable]
- public struct APIData
- {
- //key 7ebc06e7d154782906d354398eda90d2
- [SerializeField]
- private string appKey;
- public string AppKey
- {
- get
- {
- return appKey;
- }
- set
- {
- appKey = value;
- }
- }
- //id f823fd4b&
- [SerializeField]
- private string appID;
- public string AppID
- {
- get
- {
- return appID;
- }
- set
- {
- appID = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement