using UnityEngine; using UnityEngine.Networking; using System.Collections; using System.Text; using System.Collections.Generic; using LitJson; using UnityEngine.UI; public class Example : MonoBehaviour { public Text responseText; //用于显示Java接口返回的数据的文本框 private const string URL = "http://158.58.50.21:8886/view/queryFaultAndSubhealthInfo"; // 替换成实际的接口地址 void Start() { StartCoroutine(PostRequest()); //开始发送POST请求 } IEnumerator PostRequest() { UnityWebRequest request = UnityWebRequest.Post(URL, "{}"); yield return request.SendWebRequest(); Dictionary<string, object> requestData = new Dictionary<string, object>() { { "lineid", 27 }, { "areaid", 22 }, { "starttime", "2023-05-07 09:54:22" }, { "endtime", "2023-06-07 09:54:22" }, { "datatype", 1 }, { "pageSize", 5 }, { "pageNumber", 1 } }; string jsonData = JsonUtility.ToJson(requestData); //将请求参数转换为byte数组 byte[] postData = Encoding.UTF8.GetBytes(jsonData); //设置请求头 Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("Content-Type", "application/json"); //发送POST请求 UnityWebRequest www = UnityWebRequest.Post(URL, postData); www.SetRequestHeader("Content-Type", "application/json"); yield return www; if (!request.isNetworkError && request.responseCode == 200) { string json = request.downloadHandler.text; JsonData jsonData = JsonMapper.ToObject(json); int returnCode = (int)jsonData["returnCode"]; string returnMessage = (string)jsonData["returnMessage"]; if (returnCode == 0) { JsonData data = jsonData["data"]; int total = (int)data["total"]; JsonData list = data["list"]; for (int i = 0; i < list.Count; i++) { int doorid = (int)list[i]["doorid"]; string doorno = (string)list[i]["doorno"]; string faultname = (string)list[i]["faultname"]; // 解析其他字段... Debug.Log("doorid: " + doorid + ", doorno: " + doorno + ", faultname: " + faultname); } } else { Debug.Log("Error: " + returnMessage); Debug.Log("Response: " + request.downloadHandler.text); ResponseText.text = request.downloadHandler.text; } } else { Debug.Log("Error: " + request.error); } } }修改其中的错误
时间: 2024-02-14 13:12:52 浏览: 147
Simple WWW http.rar
在代码的第17行,您使用了一个错误的变量名 request,应该将其改为 www。因此,您需要将第17行修改为:
```
if (!www.isNetworkError && www.responseCode == 200)
```
此外,在第41行,您使用了错误的文本框变量名 ResponseText,应该将其改为 responseText,因此,您需要将第41行修改为:
```
responseText.text = www.downloadHandler.text;
```
修改后的完整代码如下:
```
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using LitJson;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public Text responseText; //用于显示Java接口返回的数据的文本框
private const string URL = "http://158.58.50.21:8886/view/queryFaultAndSubhealthInfo"; // 替换成实际的接口地址
void Start()
{
StartCoroutine(PostRequest()); //开始发送POST请求
}
IEnumerator PostRequest()
{
UnityWebRequest request = UnityWebRequest.Post(URL, "{}");
yield return request.SendWebRequest();
Dictionary<string, object> requestData = new Dictionary<string, object>()
{
{ "lineid", 27 },
{ "areaid", 22 },
{ "starttime", "2023-05-07 09:54:22" },
{ "endtime", "2023-06-07 09:54:22" },
{ "datatype", 1 },
{ "pageSize", 5 },
{ "pageNumber", 1 }
};
string jsonData = JsonUtility.ToJson(requestData); //将请求参数转换为byte数组
byte[] postData = Encoding.UTF8.GetBytes(jsonData);
//设置请求头
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
//发送POST请求
UnityWebRequest www = UnityWebRequest.Post(URL, postData);
www.SetRequestHeader("Content-Type", "application/json");
yield return www;
if (!www.isNetworkError && www.responseCode == 200)
{
string json = www.downloadHandler.text;
JsonData jsonData = JsonMapper.ToObject(json);
int returnCode = (int)jsonData["returnCode"];
string returnMessage = (string)jsonData["returnMessage"];
if (returnCode == 0)
{
JsonData data = jsonData["data"];
int total = (int)data["total"];
JsonData list = data["list"];
for (int i = 0; i < list.Count; i++)
{
int doorid = (int)list[i]["doorid"];
string doorno = (string)list[i]["doorno"];
string faultname = (string)list[i]["faultname"];
// 解析其他字段...
Debug.Log("doorid: " + doorid + ", doorno: " + doorno + ", faultname: " + faultname);
}
}
else
{
Debug.Log("Error: " + returnMessage);
Debug.Log("Response: " + www.downloadHandler.text);
responseText.text = www.downloadHandler.text;
}
}
else
{
Debug.Log("Error: " + www.error);
}
}
}
```
阅读全文