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; } } }为什么获得不到数据呢
时间: 2024-02-14 11:24:28 浏览: 60
有可能是以下几个原因导致获得不到数据:
1. 接口地址不正确或无法访问:请确认接口地址是否正确,并且确保网络连接正常。
2. 请求参数不正确:请检查请求参数是否正确,并且确保参数值的类型和格式正确。
3. 请求方式不正确:请检查请求方式是否正确,例如接口要求使用 GET 请求,但代码中使用了 POST 请求。
4. 返回数据格式不正确:请检查返回的 JSON 数据格式是否正确,并且确保要获取的数据在 JSON 中存在。
您可以通过打印日志等方式来排查问题,以确定具体原因。
相关问题
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,代码怎么写
这段代码中已经将请求方式改为POST了,可以直接使用。如果你需要看一下使用GET方式的代码,可以参考下面的示例代码:
```
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?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 = UnityWebRequest.Get(url);
// 发送请求
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;
}
}
}
```
使用GET方式时,请求参数是在URL中进行传递的,而不是在请求体中。需要修改的是URL中的参数,将其拼接在URL的后面即可。
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
可以将代码中的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);
}
}
}
阅读全文