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"; // 替换成实际的接口地址 IEnumerator Start() { UnityWebRequest request = UnityWebRequest.Get(URL); yield return request.SendWebRequest(); 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); } } }接口改为post
时间: 2024-04-21 21:28:18 浏览: 184
Simple WWW http.rar
可以将代码中的UnityWebRequest.Get(URL)改为UnityWebRequest.Post(URL, ""),并且在Post方法的第二个参数中传入一个空字符串,表示不需要传递任何数据。具体代码如下:
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"; // 替换成实际的接口地址
IEnumerator Start() {
UnityWebRequest request = UnityWebRequest.Post(URL, "");
yield return request.SendWebRequest();
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);
}
}
}
阅读全文