HttpResponse.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using UnityEngine;
  2. using System.Collections;
  3. using LitJson;
  4. namespace Engine.Http
  5. {
  6. /// <summary>Http消息响应对象</summary>
  7. public class HttpResponse
  8. {
  9. #region 统一的销毁创建接口 方便日后做缓存
  10. /// <summary>创建请求参数对象</summary>
  11. public static HttpResponse OnCreateResponse(string responstContent, HttpRequest request)
  12. {
  13. return new HttpResponse(responstContent, request);
  14. }
  15. /// <summary>重置消息参数对象</summary>
  16. public void Reset()
  17. {
  18. mResponstContent = string.Empty;
  19. mJsonData = null;
  20. nCode = 0;
  21. mServerTime = string.Empty;
  22. mExtTime = string.Empty;
  23. }
  24. #endregion
  25. /// <summary>响应获得内容</summary>
  26. private string mResponstContent = string.Empty;
  27. /// <summary>消息体的Json对象</summary>
  28. private JsonData mJsonData = null;
  29. /// <summary>消息号</summary>
  30. private int nCode = 0;
  31. /// <summary>服务器时间</summary>
  32. private string mServerTime = string.Empty;
  33. /// <summary>请求用时</summary>
  34. private string mExtTime = string.Empty;
  35. /// <summary>网络相应的状态值,非0为异常</summary>
  36. private int mResponseStatu;
  37. /// <summary>网络相应出现异常时附带的消息</summary>
  38. private string mResponseMsg;
  39. /// <summary>构造函数</summary>
  40. public HttpResponse(string responstContent, HttpRequest request)
  41. {
  42. Debug.Log("request.Code " + request.Code+" HttpResponse===>" +responstContent);
  43. nCode = request.Code;
  44. if(responstContent == "")
  45. {
  46. mResponseStatu = -99999;
  47. //网络请求相应的状态
  48. mResponseMsg = "网络连接不可用";
  49. return;
  50. }
  51. //创建json对象
  52. try
  53. {
  54. mJsonData = LitJson.JsonMapper.ToObject(responstContent);
  55. }
  56. catch
  57. {
  58. mResponseStatu = -99999;
  59. //网络请求相应的状态
  60. mResponseMsg = "网络连接不可用";
  61. return;
  62. }
  63. //保存响应字符串
  64. mResponstContent = responstContent;
  65. //byte[] utf8bytes = System.Text.Encoding.Default.GetBytes(mResponstContent);
  66. //mResponstContent = System.Text.Encoding.UTF8.GetString(utf8bytes);
  67. if(!mJsonData.Keys.Contains(HttpConstConfig.KEY_SERVERTIME))
  68. {
  69. CDebug.Log("这里有错");
  70. }
  71. //服务器时间
  72. mServerTime = mJsonData[HttpConstConfig.KEY_SERVERTIME].ToString();
  73. //响应时间
  74. mExtTime = (mJsonData.Keys.Contains(HttpConstConfig.KEY_EXTTIME)) ? mJsonData[HttpConstConfig.KEY_EXTTIME].ToString() : "";
  75. //网络请求相应的状态
  76. mResponseStatu = System.Convert.ToInt32(mJsonData[HttpConstConfig.KEY_RESPONSE_STATU].ToString());
  77. //网络请求相应的状态
  78. mResponseMsg = mJsonData[HttpConstConfig.KEY_RESPONSE_MSG].ToString();
  79. }
  80. /// <summary>
  81. /// 请求是否无异常
  82. /// </summary>
  83. public bool isSuccess
  84. {
  85. get
  86. {
  87. return (ResponseStatu == 0);
  88. }
  89. }
  90. /// <summary>网络请求的相应状态</summary>
  91. public int ResponseStatu
  92. {
  93. get { return mResponseStatu; }
  94. }
  95. /// <summary>网络相应发生异常时附带的消息</summary>
  96. public string ResponseMsg
  97. {
  98. get { return mResponseMsg; }
  99. }
  100. /// <summary>消息号</summary>
  101. public int Code
  102. {
  103. get { return nCode; }
  104. }
  105. /// <summary>读取一个字符串参数</summary>
  106. public string ReadString(string key)
  107. {
  108. JsonData stringData = this.ReadJsonData(key);
  109. if (stringData != null)
  110. {
  111. return stringData.ToString();
  112. }
  113. else
  114. {
  115. return string.Empty;
  116. }
  117. }
  118. /// <summary>读取一个int参数</summary>
  119. public int ReadInt(string key)
  120. {
  121. JsonData intData = this.ReadJsonData(key);
  122. if (intData != null)
  123. {
  124. return System.Convert.ToInt32(intData.ToString());
  125. }
  126. else
  127. {
  128. return -1;
  129. }
  130. }
  131. /// <summary>读取一个float参数</summary>
  132. public float ReadFloat(string key)
  133. {
  134. JsonData floatData = this.ReadJsonData(key);
  135. if (floatData != null)
  136. {
  137. return System.Convert.ToSingle(floatData.ToString());
  138. }
  139. else
  140. {
  141. return -1f;
  142. }
  143. }
  144. /// <summary>读取一个bool参数</summary>
  145. public bool ReadBool(string key)
  146. {
  147. JsonData boolData = this.ReadJsonData(key);
  148. if (boolData != null)
  149. {
  150. return System.Convert.ToBoolean(boolData.ToString());
  151. }
  152. else
  153. {
  154. return false;
  155. }
  156. }
  157. /// <summary>读取一个JsonData参数</summary>
  158. public JsonData ReadJsonData(string key = null)
  159. {
  160. if(mJsonData == null)
  161. return null;
  162. try
  163. {
  164. if (key == null)
  165. return mJsonData[HttpConstConfig.KEY_RESULT];
  166. if (mJsonData != null && mJsonData[HttpConstConfig.KEY_RESULT] != null && mJsonData[HttpConstConfig.KEY_RESULT][key] != null)
  167. {
  168. return mJsonData[HttpConstConfig.KEY_RESULT][key];
  169. }
  170. else
  171. {
  172. return null;
  173. }
  174. }
  175. catch(System.Exception e)
  176. {
  177. return null;
  178. }
  179. }
  180. public string ErrorString()
  181. {
  182. return mJsonData[HttpConstConfig.KEY_RESULT].ToString();
  183. }
  184. }
  185. }