Unity 之 使用UnityWebRequest.Get结果得到"Accept"的问题记录
【摘要】 Unity 使用UnityWebRequest.Get(”0“)在真机上获得结果”Accept“;
一,出现问题
在使用下面这个协程时,传值是“0” ,正常的测试结果request.isNetworkError
为True 结束协程:
报错代码:
IEnumerator GetUrlData(string url)
{
Debug.Log("GetADImageList url: " + url);
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
yield break;
}
if (string.IsNullOrEmpty(request.downloadHandler.text))
{
yield break;
}
JsonData data = JsonMapper.ToObject(request.downloadHandler.text);
// todo...
}
二,报错信息:
在真机Oppo R9 上出现的问题,在其他机型的手机没有出现这个问题:
>GetADImageList url: 0
>GetADImageList IsNullOrEmpty Accept
>输出condition:JsonException: Invalid character 'A' in input string,输出stackTrace:LitJson.Lexer.NextToken () (at <00000000000000000000000000000000>:0)
LitJson.JsonReader.ReadToken () (at <00000000000000000000000000000000>:0)
LitJson.JsonReader.Read () (at <00000000000000000000000000000000>:0)
LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader) (at <00000000000000000000000000000000>:0)
LitJson.JsonMapper.ToObject (System.String json) (at <00000000000000000000000000000000>:0)
ADSDK+<GetADImageList>d__104.MoveNext () (at <00000000000000000000000000000000>:0)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <00000000000000000000000000000000>:0)
三,修正代码:
后来没有分析出原因,也没有找到解决方案,只能在后面使用获得结果的时候加个异常捕获:
IEnumerator GetUrlData(string url)
{
Debug.Log("GetADImageList url: " + url);
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
yield break;
}
if (string.IsNullOrEmpty(request.downloadHandler.text))
{
yield break;
}
JsonData data;
try
{
data = JsonMapper.ToObject(request.downloadHandler.text);
}
catch (Exception e)
{
Debug.Log("GetADImageList request.downloadHandler.text: " + request.downloadHandler.text);
throw e;
}
// todo...
}
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)