HttpResponse.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. CDebug.Log(responstContent);
  43. nCode = request.Code;
  44. if(responstContent == "")
  45. {
  46. mResponseStatu = -99999;
  47. //网络请求相应的状态
  48. mResponseMsg = "网络连接不可用";
  49. return;
  50. }
  51. //保存响应字符串
  52. mResponstContent = responstContent;
  53. //byte[] utf8bytes = System.Text.Encoding.Default.GetBytes(mResponstContent);
  54. //mResponstContent = System.Text.Encoding.UTF8.GetString(utf8bytes);
  55. //创建json对象
  56. mJsonData = LitJson.JsonMapper.ToObject(mResponstContent);
  57. if(!mJsonData.Keys.Contains(HttpConstConfig.KEY_SERVERTIME))
  58. {
  59. CDebug.Log("这里有错");
  60. }
  61. //服务器时间
  62. mServerTime = mJsonData[HttpConstConfig.KEY_SERVERTIME].ToString();
  63. //响应时间
  64. mExtTime = (mJsonData.Keys.Contains(HttpConstConfig.KEY_EXTTIME)) ? mJsonData[HttpConstConfig.KEY_EXTTIME].ToString() : "";
  65. //网络请求相应的状态
  66. mResponseStatu = System.Convert.ToInt32(mJsonData[HttpConstConfig.KEY_RESPONSE_STATU].ToString());
  67. //网络请求相应的状态
  68. mResponseMsg = mJsonData[HttpConstConfig.KEY_RESPONSE_MSG].ToString();
  69. }
  70. /// <summary>
  71. /// 请求是否无异常
  72. /// </summary>
  73. public bool isSuccess
  74. {
  75. get
  76. {
  77. return (ResponseStatu == 0);
  78. }
  79. }
  80. /// <summary>网络请求的相应状态</summary>
  81. public int ResponseStatu
  82. {
  83. get { return mResponseStatu; }
  84. }
  85. /// <summary>网络相应发生异常时附带的消息</summary>
  86. public string ResponseMsg
  87. {
  88. get { return mResponseMsg; }
  89. }
  90. /// <summary>消息号</summary>
  91. public int Code
  92. {
  93. get { return nCode; }
  94. }
  95. /// <summary>读取一个字符串参数</summary>
  96. public string ReadString(string key)
  97. {
  98. JsonData stringData = this.ReadJsonData(key);
  99. if (stringData != null)
  100. {
  101. return stringData.ToString();
  102. }
  103. else
  104. {
  105. return string.Empty;
  106. }
  107. }
  108. /// <summary>读取一个int参数</summary>
  109. public int ReadInt(string key)
  110. {
  111. JsonData intData = this.ReadJsonData(key);
  112. if (intData != null)
  113. {
  114. return System.Convert.ToInt32(intData.ToString());
  115. }
  116. else
  117. {
  118. return -1;
  119. }
  120. }
  121. /// <summary>读取一个float参数</summary>
  122. public float ReadFloat(string key)
  123. {
  124. JsonData floatData = this.ReadJsonData(key);
  125. if (floatData != null)
  126. {
  127. return System.Convert.ToSingle(floatData.ToString());
  128. }
  129. else
  130. {
  131. return -1f;
  132. }
  133. }
  134. /// <summary>读取一个bool参数</summary>
  135. public bool ReadBool(string key)
  136. {
  137. JsonData boolData = this.ReadJsonData(key);
  138. if (boolData != null)
  139. {
  140. return System.Convert.ToBoolean(boolData.ToString());
  141. }
  142. else
  143. {
  144. return false;
  145. }
  146. }
  147. /// <summary>读取一个JsonData参数</summary>
  148. public JsonData ReadJsonData(string key = null)
  149. {
  150. if(mJsonData == null)
  151. return null;
  152. try
  153. {
  154. if (key == null)
  155. return mJsonData[HttpConstConfig.KEY_RESULT];
  156. if (mJsonData != null && mJsonData[HttpConstConfig.KEY_RESULT] != null && mJsonData[HttpConstConfig.KEY_RESULT][key] != null)
  157. {
  158. return mJsonData[HttpConstConfig.KEY_RESULT][key];
  159. }
  160. else
  161. {
  162. return null;
  163. }
  164. }
  165. catch(System.Exception e)
  166. {
  167. return null;
  168. }
  169. }
  170. public string ErrorString()
  171. {
  172. return mJsonData[HttpConstConfig.KEY_RESULT].ToString();
  173. }
  174. }
  175. }