using System.Collections; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using LitJson; using System.Collections.Generic; public class GetData : MonoBehaviour { public Text resultText; IEnumerator Start() { // 定义接口地址和请求参数 string url = "http://158.58.50.21:8886/view/queryFaultAndSubhealthInfo"; string json = "{"lineid": 27, "areaid": 22, "starttime": "2023-05-07 09:54:22", "endtime": "2023-06-07 09:54:22", "datatype": 2, "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.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; } } }将请求方式改为post,java接口,unity webgl代码怎么写
时间: 2023-07-21 17:58:24 浏览: 196
Java 接口代码已经在上一个问题中给出,这里提供 Unity WebGL 的代码实现:
```
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using LitJson;
using System.Collections.Generic;
public class GetData : MonoBehaviour {
public Text resultText;
IEnumerator Start() {
// 定义接口地址
string url = "http://158.58.50.21:8886/view/queryFaultAndSubhealthInfo";
// 构造请求参数
Dictionary<string, object> requestData = new Dictionary<string, object>();
requestData["lineid"] = 27;
requestData["areaid"] = 22;
requestData["starttime"] = "2023-05-07 09:54:22";
requestData["endtime"] = "2023-06-07 09:54:22";
requestData["datatype"] = 2;
requestData["pageSize"] = 5;
requestData["pageNumber"] = 1;
string json = JsonMapper.ToJson(requestData);
// 构造请求
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.isNetworkError) {
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;
}
}
}
```
这里使用了 `Dictionary<string, object>` 来构造请求参数,并使用 `JsonMapper.ToJson` 方法将其转换为 JSON 格式的字符串。同时,需要注意在判断请求是否出错时,使用的是 `request.isNetworkError` 而不是 `request.isError`。
阅读全文