网络异常处理
This commit is contained in:
parent
8c5ad08475
commit
c0ebe80a1f
@ -16,23 +16,41 @@ namespace Assets.Scripts.Apis
|
||||
protected async override Task<HttpResponseMessage> SendAsync(
|
||||
HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
//{"result":false,"errMsg":"已在其他设备登录","code":401}
|
||||
var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
if (request.RequestUri.ToString().Contains(App.Host) && result.Contains(@"{""result"":false,"))
|
||||
try
|
||||
{
|
||||
var json = ParseJson<JsonResult<object>>(result);
|
||||
if (json.code == 401)
|
||||
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
//{"result":false,"errMsg":"已在其他设备登录","code":401}
|
||||
var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
if (request.RequestUri.ToString().Contains(App.Host) && result.Contains(@"{""result"":false,"))
|
||||
{
|
||||
App.is401 = json.errMsg;
|
||||
UnityMainThreadDispatcher.Instance().Enqueue(LoadLogin(json.errMsg));
|
||||
var json = ParseJson<JsonResult<object>>(result);
|
||||
if (json.code == 401)
|
||||
{
|
||||
App.is401 = json.errMsg;
|
||||
UnityMainThreadDispatcher.Instance().Enqueue(LoadLogin(json.errMsg));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
App.is401 = null;
|
||||
}
|
||||
if (response.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
response.Content = new StringContent(new JsonResult<object> { result = false, errMsg = "Please check if the network connection is normal.", data = null }.ToString());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
else
|
||||
catch (Exception e)
|
||||
{
|
||||
App.is401 = null;
|
||||
Debug.Log(e);
|
||||
HttpResponseMessage httpResponse = new HttpResponseMessage
|
||||
{
|
||||
StatusCode = System.Net.HttpStatusCode.BadRequest,
|
||||
Content = new StringContent(new JsonResult<object> { result = false, errMsg = "Please check if the network connection is normal.", data = null }.ToString())
|
||||
};
|
||||
return httpResponse;
|
||||
}
|
||||
return response;
|
||||
|
||||
}
|
||||
private T ParseJson<T>(string result)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user