Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. using System.IO;
  7. using SimpleJSON;
  8.  
  9. [System.Serializable]
  10. public class JSONManager : Manager
  11. {
  12. // API Url
  13. private const string URL = "https://external.api.yle.fi/v1/programs/items.json?";
  14.  
  15. //limit of the loaded items
  16. private const int limit = 10;
  17.  
  18. //Search bar
  19. [SerializeField]
  20. private Text search;
  21.  
  22. //Api data (API key and Api ID)
  23. [SerializeField]
  24. private APIData APIInfo;
  25.  
  26. //startindex
  27. [SerializeField]
  28. private int offset = 0;
  29. public int Offset
  30. {
  31. get
  32. {
  33. return offset;
  34. }
  35. set
  36. {
  37. offset = value;
  38. }
  39. }
  40.  
  41. private UIManager uiManager
  42. {
  43. get
  44. {
  45. return GameManager.Instance.GetManager<UIManager>();
  46. }
  47. }
  48.  
  49. /// <summary>
  50. /// This function execute when the user click on the search
  51. /// </summary>
  52. public void StartRequest()
  53. {
  54. GameManager.Instance.GetManager<JSONManager>().Offset = 0;
  55. GameManager.Instance.StartCoroutine(RequestAPI());
  56. }
  57.  
  58. /// <summary>
  59. /// Creating the URL.
  60. /// </summary>
  61. /// <param name="query">Searching query</param>
  62. private string CreateUrl(string query, int limit,int offset)
  63. {
  64. Debug.Log(offset);
  65. return URL + "q=" + query + "&offset=" + offset + "&limit=" + limit +"&app_id=" + APIInfo.AppID + "&app_key=" + APIInfo.AppKey;
  66. }
  67.  
  68. /// <summary>
  69. /// Requestng the Api
  70. /// </summary>
  71. /// <returns></returns>
  72. private IEnumerator RequestAPI()
  73. {
  74. //Clear the UI Items
  75. uiManager.ClearItems();
  76.  
  77. //Requesting API
  78. UnityWebRequest Requesting = UnityWebRequest.Get(CreateUrl(search.text,limit, offset));
  79.  
  80. yield return Requesting.Send();
  81. //check if the request doesnt have errors
  82. if (!Requesting.isNetworkError || !Requesting.isHttpError || !Requesting.isNetworkError || Requesting != null)
  83. {
  84. //Parse the UnityWebRequest to an object.
  85. JSONNode node = JSONArray.Parse(Requesting.downloadHandler.text).AsObject;
  86.  
  87. //checking if the node is find
  88. if (node == null)
  89. Debug.Log("NULL");
  90.  
  91. //list of 10 items
  92. List<YleItem> items = new List<YleItem>();
  93.  
  94. for (int i = 0; i < node[2].Count; i++)
  95. {
  96. //getting the title and description.
  97. string title = node["data"][i]["title"]["fi"];
  98. string description = node["data"][i]["description"]["fi"];
  99.  
  100. //adding it and making it a object
  101. items.Add(new YleItem(title, description));
  102. }
  103.  
  104. //Updating the items.
  105. for (int i = 0; i < node[2].Count; i++)
  106. uiManager.UpdateItems(items);
  107. }
  108.  
  109. else
  110. {
  111. Debug.LogError("Error");
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// all the API data
  117. /// </summary>
  118. [System.Serializable]
  119. public struct APIData
  120. {
  121. //key 7ebc06e7d154782906d354398eda90d2
  122. [SerializeField]
  123. private string appKey;
  124. public string AppKey
  125. {
  126. get
  127. {
  128. return appKey;
  129. }
  130. set
  131. {
  132. appKey = value;
  133. }
  134. }
  135.  
  136. //id f823fd4b&
  137. [SerializeField]
  138. private string appID;
  139. public string AppID
  140. {
  141. get
  142. {
  143. return appID;
  144. }
  145. set
  146. {
  147. appID = value;
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement