123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using UnityEngine;
- using System.Collections;
- using LitJson;
- namespace Engine.Http
- {
- /// <summary>Http消息响应对象</summary>
- public class HttpResponse
- {
- #region 统一的销毁创建接口 方便日后做缓存
- /// <summary>创建请求参数对象</summary>
- public static HttpResponse OnCreateResponse(string responstContent, HttpRequest request)
- {
- return new HttpResponse(responstContent, request);
- }
- /// <summary>重置消息参数对象</summary>
- public void Reset()
- {
- mResponstContent = string.Empty;
- mJsonData = null;
- nCode = 0;
- mServerTime = string.Empty;
- mExtTime = string.Empty;
- }
- #endregion
-
- /// <summary>响应获得内容</summary>
- private string mResponstContent = string.Empty;
- /// <summary>消息体的Json对象</summary>
- private JsonData mJsonData = null;
- /// <summary>消息号</summary>
- private int nCode = 0;
- /// <summary>服务器时间</summary>
- private string mServerTime = string.Empty;
- /// <summary>请求用时</summary>
- private string mExtTime = string.Empty;
- /// <summary>网络相应的状态值,非0为异常</summary>
- private int mResponseStatu;
- /// <summary>网络相应出现异常时附带的消息</summary>
- private string mResponseMsg;
- /// <summary>构造函数</summary>
- public HttpResponse(string responstContent, HttpRequest request)
- {
- CDebug.Log(responstContent);
- nCode = request.Code;
- if(responstContent == "")
- {
- mResponseStatu = -99999;
- //网络请求相应的状态
- mResponseMsg = "网络连接不可用";
- return;
- }
- //保存响应字符串
- mResponstContent = responstContent;
- //byte[] utf8bytes = System.Text.Encoding.Default.GetBytes(mResponstContent);
- //mResponstContent = System.Text.Encoding.UTF8.GetString(utf8bytes);
-
- //创建json对象
- mJsonData = LitJson.JsonMapper.ToObject(mResponstContent);
- if(!mJsonData.Keys.Contains(HttpConstConfig.KEY_SERVERTIME))
- {
- CDebug.Log("这里有错");
- }
- //服务器时间
- mServerTime = mJsonData[HttpConstConfig.KEY_SERVERTIME].ToString();
- //响应时间
- mExtTime = (mJsonData.Keys.Contains(HttpConstConfig.KEY_EXTTIME)) ? mJsonData[HttpConstConfig.KEY_EXTTIME].ToString() : "";
- //网络请求相应的状态
- mResponseStatu = System.Convert.ToInt32(mJsonData[HttpConstConfig.KEY_RESPONSE_STATU].ToString());
- //网络请求相应的状态
- mResponseMsg = mJsonData[HttpConstConfig.KEY_RESPONSE_MSG].ToString();
- }
- /// <summary>
- /// 请求是否无异常
- /// </summary>
- public bool isSuccess
- {
- get
- {
- return (ResponseStatu == 0);
- }
- }
- /// <summary>网络请求的相应状态</summary>
- public int ResponseStatu
- {
- get { return mResponseStatu; }
- }
- /// <summary>网络相应发生异常时附带的消息</summary>
- public string ResponseMsg
- {
- get { return mResponseMsg; }
- }
- /// <summary>消息号</summary>
- public int Code
- {
- get { return nCode; }
- }
- /// <summary>读取一个字符串参数</summary>
- public string ReadString(string key)
- {
- JsonData stringData = this.ReadJsonData(key);
- if (stringData != null)
- {
- return stringData.ToString();
- }
- else
- {
- return string.Empty;
- }
- }
- /// <summary>读取一个int参数</summary>
- public int ReadInt(string key)
- {
- JsonData intData = this.ReadJsonData(key);
- if (intData != null)
- {
- return System.Convert.ToInt32(intData.ToString());
- }
- else
- {
- return -1;
- }
- }
- /// <summary>读取一个float参数</summary>
- public float ReadFloat(string key)
- {
- JsonData floatData = this.ReadJsonData(key);
- if (floatData != null)
- {
- return System.Convert.ToSingle(floatData.ToString());
- }
- else
- {
- return -1f;
- }
- }
- /// <summary>读取一个bool参数</summary>
- public bool ReadBool(string key)
- {
- JsonData boolData = this.ReadJsonData(key);
- if (boolData != null)
- {
- return System.Convert.ToBoolean(boolData.ToString());
- }
- else
- {
- return false;
- }
- }
- /// <summary>读取一个JsonData参数</summary>
- public JsonData ReadJsonData(string key = null)
- {
- if(mJsonData == null)
- return null;
- try
- {
- if (key == null)
- return mJsonData[HttpConstConfig.KEY_RESULT];
- if (mJsonData != null && mJsonData[HttpConstConfig.KEY_RESULT] != null && mJsonData[HttpConstConfig.KEY_RESULT][key] != null)
- {
- return mJsonData[HttpConstConfig.KEY_RESULT][key];
- }
- else
- {
- return null;
- }
- }
- catch(System.Exception e)
- {
- return null;
- }
- }
- public string ErrorString()
- {
- return mJsonData[HttpConstConfig.KEY_RESULT].ToString();
- }
- }
- }
|