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