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(); 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请求 WWW www = new WWW(javaAPIUrl, postData, headers); 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 15:32:49 浏览: 171
在代码中有几处错误需要修改:
1.第6行:将responseText改为ResponseText,与代码中的变量名一致。
2.第11行:将UnityWebRequest.Post(URL, "")改为UnityWebRequest.Post(URL, "{}"),需要传递一个空的JSON对象作为请求体。
3.第17行:将WWW改为UnityWebRequest,与UnityWebRequest.Post配套使用。
4.第17行:将javaAPIUrl改为URL,与第11行中的URL变量名一致。
5.第25行:将requestData改为postData,将请求参数转换为byte数组的变量名应为postData。
6.第28行:将request.responseCode改为request.responseCode,获取HTTP响应状态码应该使用request.responseCode。
修改后的代码如下:
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();
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);
}
}
}
阅读全文