URLRequest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. public URLRequestBytesCallBackDelegate bytesCallBack;
  19. // Use this for initialization
  20. IEnumerator Start() {
  21. WWW www = (form == null ? new WWW(url) : new WWW(url, form));
  22. yield return www;
  23. string data = null;
  24. Debuger.LogWarning("Url loaded : "+url);
  25. if(www.error != null)
  26. {
  27. data = www.error;
  28. Debuger.LogError("Url error : "+www.error);
  29. }
  30. else
  31. {
  32. data = www.text;
  33. Debuger.LogWarning("Url result : "+www.text);
  34. }
  35. if(callBack != null)
  36. callBack(data);
  37. if(jsonCallBack != null)
  38. jsonCallBack(ParseRecieveJsonData(data));
  39. if (bytesCallBack != null)
  40. bytesCallBack (www.bytes);
  41. Destroy(this.gameObject);
  42. }
  43. public delegate void URLRequestCallBackDelegate(string data);
  44. public delegate void URLRequestJsonCallBackDelegate(JsonData data);
  45. public delegate void URLRequestBytesCallBackDelegate (Byte[] bytes);
  46. public static URLRequest CreateStrURLRequest(string url, URLRequestData data = null, URLRequestCallBackDelegate callBack = null, Method method = Method.GET)
  47. {
  48. URLRequest urlRequest = CreateBaseURLRequest(url, data, method);
  49. urlRequest.callBack = callBack;
  50. return urlRequest;
  51. }
  52. public static URLRequest CreateURLRequest(string url, URLRequestData data = null, URLRequestJsonCallBackDelegate callBack = null, Method method = Method.GET)
  53. {
  54. URLRequest urlRequest = CreateBaseURLRequest(url, data, method);
  55. urlRequest.jsonCallBack = callBack;
  56. return urlRequest;
  57. }
  58. public static URLRequest CreateBytesURLRequest(string url, URLRequestData data = null, URLRequestBytesCallBackDelegate callBack = null, Method method = Method.GET)
  59. {
  60. URLRequest urlRequest = CreateBaseURLRequest(url, data, method);
  61. urlRequest.bytesCallBack = callBack;
  62. return urlRequest;
  63. }
  64. private static URLRequest CreateBaseURLRequest(string url, URLRequestData requestData = null, Method method = Method.GET)
  65. {
  66. GameObject gameObj = new GameObject("URLRequest");
  67. URLRequest urlRequest = gameObj.AddComponent<URLRequest>();
  68. Debuger.Log("Url request : "+url);
  69. if(requestData != null && requestData.HasData())
  70. {
  71. if(method == Method.GET)
  72. {
  73. if(url.IndexOf("?") == -1)
  74. url += "?";
  75. if(url.Substring(url.Length-1) != "?")
  76. url += "&";
  77. url += requestData.GetDataString();
  78. }
  79. else if(method == Method.POST)
  80. {
  81. urlRequest.form = requestData.GetDataForm();
  82. }
  83. }
  84. urlRequest.url = url;
  85. return urlRequest;
  86. }
  87. public static JsonData ParseRecieveJsonData(string data)
  88. {
  89. int index = data.IndexOf("{");
  90. if(index >= 0)
  91. {
  92. data = data.Substring(index);
  93. }
  94. try
  95. {
  96. return JsonMapper.ToObject(data);
  97. }
  98. catch(JsonException e)
  99. {
  100. Debuger.LogException(e);
  101. }
  102. JsonData json = new JsonData();
  103. json["c"] = -1;
  104. json["d"] = data;
  105. return json;
  106. }
  107. }