IEnumerator Start() { // 定义接口地址和请求参数 string url = "http://your-interface-url.com"; string json = "{\"lineid\": 27, \"areaid\": 22, \"starttime\": \"2023-05-07 09:54:22\", \"endtime\": \"2023-06-07 09:54:22\", \"datatype\": 1, \"pageSize\": 5, \"pageNumber\": 1 }"; // 构造请求 UnityWebRequest request = new UnityWebRequest(url, "POST"); byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json); request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); // 发送请求 yield return request.SendWebRequest(); // 处理返回结果 if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError) { Debug.Log(request.error); resultText.text = request.error; } else { // 解析返回的 JSON 数据 JsonData jsonData = JsonMapper.ToObject(request.downloadHandler.text); // 获取需要的数据,将其显示在文本框中 string text = ""; foreach (JsonData item in jsonData["data"]["list"]) { text += item["doorno"].ToString() + " - " + item["faultname"].ToString() + " - " + item["happentime"].ToString() + "\n"; } resultText.text = text; } }怎么替换result适应老版本unity
时间: 2024-02-14 18:24:36 浏览: 78
根据UnityWebRequest的官方文档,旧版本的UnityWebRequest类没有result属性,而是使用isError属性来判断请求是否出错,使用error属性获取错误信息。因此,你可以将if语句中的判断条件改为request.isError,将Debug.Log和resultText.text的赋值改为request.error即可,例如:
```
// 处理返回结果
if (request.isError)
{
Debug.Log(request.error);
resultText.text = request.error;
}
else
{
// 解析返回的 JSON 数据
JsonData jsonData = JsonMapper.ToObject(request.downloadHandler.text);
// 获取需要的数据,将其显示在文本框中
string text = "";
foreach (JsonData item in jsonData["data"]["list"])
{
text += item["doorno"].ToString() + " - " + item["faultname"].ToString() + " - " + item["happentime"].ToString() + "\n";
}
resultText.text = text;
}
```
阅读全文