WebRequestBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 
  2. using LitJson;
  3. using System;
  4. using System.Collections;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. /// <summary>
  8. /// 一个WebRequest的基类
  9. /// </summary>
  10. public abstract class WebRequestBase {
  11. public string url;
  12. public WWWForm wwwForm;
  13. public WebRequestType webRequestType;
  14. protected JsonData responseJsonData;
  15. protected Action<JsonData> mSuccess;
  16. protected Action<JsonData> mFailed;
  17. protected Action<JsonData> mErrorCallBack;
  18. public enum WebRequestType {
  19. GET,
  20. POST,
  21. PUT,
  22. }
  23. public string LOGTAG = "[WebRequestBase]:";
  24. public WebRequestBase(string _url, WWWForm _form, Action<JsonData> success, Action<JsonData> failed, Action<JsonData> errorCallBack=null) {
  25. url = _url;
  26. wwwForm = _form;
  27. mSuccess = success;
  28. mFailed = failed;
  29. mErrorCallBack = errorCallBack;
  30. }
  31. //public abstract void Success(JsonData responseJson);
  32. //public abstract void Failed(JsonData responseJson);
  33. public virtual void NetWorkError(string errText) {
  34. Debug.Log(LOGTAG + "NetWorkError");
  35. //try {
  36. // UISystem.Instant.PushUIPanel(UIPanelsType.InfoType1Panel, "ERROR: NetWorkError,Please Open Wifi");
  37. //} catch (Exception e) {
  38. // Debug.Log(e);
  39. //}
  40. }
  41. public virtual IEnumerator SendRequest() {
  42. if (webRequestType == WebRequestType.GET) {
  43. UnityWebRequest www = UnityWebRequest.Get(url);
  44. yield return www.SendWebRequest();
  45. if (www.isNetworkError || www.isHttpError) {
  46. Debug.Log(www.error);
  47. //responseJsonData = JsonMapper.ToObject(www.error);
  48. NetWorkError(www.error);
  49. } else {
  50. Debug.Log(www.downloadHandler.text);
  51. responseJsonData = JsonMapper.ToObject(www.downloadHandler.text);
  52. if (mSuccess != null) {
  53. mSuccess(responseJsonData);
  54. }
  55. }
  56. } else if (webRequestType == WebRequestType.POST) {
  57. try {
  58. //if (UserSystem.Instant.SysInfo.UserToken != null) {
  59. // wwwForm.AddField("token", UserSystem.Instant.SysInfo.UserToken);
  60. // Debug.Log("[" + GetType().ToString() + "]: " + "Form Add UserToken !");
  61. //}
  62. } catch(Exception e) {
  63. Debug.Log(e+ "\nUserSystem.Instant.SysInfo.UserToken is Null");
  64. }
  65. Debug.Log("[" + GetType().ToString() + "]: " + "SendRequest:" + "URL:" + url + "\nFormData:" + System.Text.Encoding.Default.GetString(wwwForm.data));
  66. UnityWebRequest www = UnityWebRequest.Post(url, wwwForm);
  67. yield return www.SendWebRequest();
  68. if (www.isNetworkError || www.isHttpError) {
  69. Debug.Log(www.error);
  70. //responseJsonData = JsonMapper.ToObject(www.error);
  71. if (mErrorCallBack!=null) {
  72. mErrorCallBack(www.error);
  73. }
  74. NetWorkError(www.error);
  75. } else {
  76. Debug.Log("[" + GetType().ToString() + "]: \n" + www.downloadHandler.text);
  77. try {
  78. responseJsonData = JsonMapper.ToObject(www.downloadHandler.text);
  79. if ("200" == responseJsonData["code"].ToString()) {
  80. if (mSuccess != null) {
  81. mSuccess(responseJsonData);
  82. }
  83. } else if ("-1" == responseJsonData["code"].ToString()) {
  84. try {
  85. //if (responseJsonData["msg"].ToString() == "验证失败,请重新获取系统token") {
  86. // UserSystem.Instant.SysInfo.ResetUserInfo();
  87. //}
  88. } catch (Exception e) {
  89. Debug.Log(e);
  90. }
  91. if (mFailed != null) {
  92. mFailed(responseJsonData);
  93. }
  94. } else{
  95. if (mFailed != null) {
  96. mFailed(responseJsonData);
  97. }
  98. }
  99. } catch (Exception e) {
  100. Debug.Log(e);
  101. }
  102. }
  103. }
  104. }
  105. }