URLRequest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using UnityEngine;
  2. using LitJson;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public class URLRequest : MonoBehaviour
  7. {
  8. public enum Method
  9. {
  10. GET,
  11. POST,
  12. }
  13. public string url;
  14. public Method method;
  15. public WWWForm form;
  16. public URLRequestCallBackDelegate callBack;
  17. public URLRequestJsonCallBackDelegate jsonCallBack;
  18. // Use this for initialization
  19. IEnumerator Start() {
  20. WWW www = (form == null ? new WWW(url) : new WWW(url, form));
  21. yield return www;
  22. string data = null;
  23. Debuger.LogWarning("Url loaded : "+url);
  24. if(www.error != null)
  25. {
  26. data = www.error;
  27. Debuger.LogError("Url error : "+www.error);
  28. }
  29. else
  30. {
  31. data = www.text;
  32. Debuger.LogWarning("Url result : "+www.text);
  33. }
  34. if(callBack != null)
  35. callBack(data);
  36. if(jsonCallBack != null)
  37. jsonCallBack(ParseRecieveJsonData(data));
  38. URLGOs.Remove(gameObject);
  39. Destroy(this.gameObject);
  40. }
  41. public delegate void URLRequestCallBackDelegate(string data);
  42. public delegate void URLRequestJsonCallBackDelegate(JsonData data);
  43. public static URLRequest CreateStrURLRequest(bool important, string url, URLRequestData data = null, URLRequestCallBackDelegate callBack = null, Method method = Method.GET, bool dataEye = false)
  44. {
  45. if (URLGOs.Count > MaxNotImportantURLAmt && !important)
  46. {
  47. return null;
  48. }
  49. URLRequest urlRequest = CreateBaseURLRequest(url, data, method, dataEye);
  50. urlRequest.callBack = callBack;
  51. return urlRequest;
  52. }
  53. private static int MaxNotImportantURLAmt = 10;
  54. private static List<GameObject> URLGOs = new List<GameObject>();
  55. public static URLRequest CreateURLRequest(bool important, string url, URLRequestData data = null, URLRequestJsonCallBackDelegate callBack = null, Method method = Method.GET)
  56. {
  57. if (URLGOs.Count > MaxNotImportantURLAmt && !important)
  58. {
  59. return null;
  60. }
  61. URLRequest urlRequest = CreateBaseURLRequest(url, data, method);
  62. urlRequest.jsonCallBack = callBack;
  63. return urlRequest;
  64. }
  65. private static URLRequest CreateBaseURLRequest(string url, URLRequestData requestData = null, Method method = Method.GET, bool dataEye = false)
  66. {
  67. GameObject gameObj = new GameObject("URLRequest");
  68. DontDestroyOnLoad(gameObj);
  69. URLGOs.Add(gameObj);
  70. URLRequest urlRequest = gameObj.AddComponent<URLRequest>();
  71. Debuger.Log("Url request : "+url);
  72. if(!dataEye && requestData.HasData())
  73. {
  74. if(method == Method.GET)
  75. {
  76. if(url.IndexOf("?") == -1)
  77. url += "?";
  78. if(url.Substring(url.Length-1) != "?")
  79. url += "&";
  80. url += requestData.GetDataString();
  81. }
  82. else if(method == Method.POST)
  83. {
  84. urlRequest.form = requestData.GetDataForm();
  85. }
  86. }
  87. urlRequest.url = url;
  88. return urlRequest;
  89. }
  90. public static JsonData ParseRecieveJsonData(string data)
  91. {
  92. int index = data.IndexOf("{");
  93. if(index >= 0)
  94. {
  95. data = data.Substring(index);
  96. }
  97. try
  98. {
  99. return JsonMapper.ToObject(data);
  100. }
  101. catch(JsonException e)
  102. {
  103. Debuger.LogException(e);
  104. //Debug.Log(e.ToString());
  105. //Debug.Log(data);
  106. }
  107. JsonData json = new JsonData();
  108. json["error"] = 0;
  109. return json;
  110. }
  111. }