URLRequest.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Destroy(this.gameObject);
  39. }
  40. public delegate void URLRequestCallBackDelegate(string data);
  41. public delegate void URLRequestJsonCallBackDelegate(JsonData data);
  42. public static URLRequest CreateStrURLRequest(string url, URLRequestData data = null, URLRequestCallBackDelegate callBack = null, Method method = Method.GET, bool dataEye = false)
  43. {
  44. URLRequest urlRequest = CreateBaseURLRequest(url, data, method, dataEye);
  45. urlRequest.callBack = callBack;
  46. return urlRequest;
  47. }
  48. public static URLRequest CreateURLRequest(string url, URLRequestData data = null, URLRequestJsonCallBackDelegate callBack = null, Method method = Method.GET)
  49. {
  50. URLRequest urlRequest = CreateBaseURLRequest(url, data, method);
  51. urlRequest.jsonCallBack = callBack;
  52. return urlRequest;
  53. }
  54. private static URLRequest CreateBaseURLRequest(string url, URLRequestData requestData = null, Method method = Method.GET, bool dataEye = false)
  55. {
  56. GameObject gameObj = new GameObject("URLRequest");
  57. URLRequest urlRequest = gameObj.AddComponent<URLRequest>();
  58. Debuger.Log("Url request : "+url);
  59. if(!dataEye && requestData.HasData())
  60. {
  61. if(method == Method.GET)
  62. {
  63. if(url.IndexOf("?") == -1)
  64. url += "?";
  65. if(url.Substring(url.Length-1) != "?")
  66. url += "&";
  67. url += requestData.GetDataString();
  68. }
  69. else if(method == Method.POST)
  70. {
  71. urlRequest.form = requestData.GetDataForm();
  72. }
  73. }
  74. urlRequest.url = url;
  75. return urlRequest;
  76. }
  77. public static JsonData ParseRecieveJsonData(string data)
  78. {
  79. int index = data.IndexOf("{");
  80. if(index >= 0)
  81. {
  82. data = data.Substring(index);
  83. }
  84. try
  85. {
  86. return JsonMapper.ToObject(data);
  87. }
  88. catch(JsonException e)
  89. {
  90. Debuger.LogException(e);
  91. }
  92. JsonData json = new JsonData();
  93. json["error"] = 0;
  94. return json;
  95. }
  96. }