// 构造请求 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; }替换掉result,unity版本太老,总是报错
时间: 2024-03-27 18:39:49 浏览: 206
如果您的 Unity 版本太老,无法使用 Result 枚举类型,您可以使用以下方式替换代码中的 `request.isNetworkError`:
```
// 构造请求
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.Send();
// 处理返回结果
if (request.isError)
{
Debug.Log(request.error);
resultText.text = request.error;
}
```
这段代码中使用了 `request.isError` 来判断是否发生网络错误。如果 `isError` 为 true,则表示发生了网络错误,可以通过 `request.error` 获取错误信息。
相关问题
using UnityEngine; using UnityEngine.Networking; using System.Collections; using System.Collections.Generic; using LitJson; public class Example : MonoBehaviour { public UnityEngine.UI.Text resultText; void Start() { StartCoroutine(PostRequest()); } IEnumerator PostRequest() { string url = "your_post_url_here"; string postData = "your_post_data_here"; // Send POST request UnityWebRequest request = new UnityWebRequest(url, "POST"); byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(postData); request.uploadHandler = new UploadHandlerRaw(bodyRaw); request.downloadHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return request.SendWebRequest(); // Parse response if (request.result == UnityWebRequest.Result.Success) { string json = request.downloadHandler.text; JsonData data = JsonMapper.ToObject(json); string result = "Return Code: " + data["returnCode"] + "\n"; result += "Return Message: " + data["returnMessage"] + "\n"; result += "Data: " + data["data"] + "\n"; resultText.text = result; } else { Debug.Log(request.error); } } }修改代码
根据你提供的代码,可能会有以下几个问题:
1. 在 `UnityWebRequest` 类的构造函数中,第一个参数应该是请求的 URL 地址,而不是字符串 `"POST"`。你需要将 `url` 变量作为第一个参数传递给构造函数。
2. 在 `UnityWebRequest` 类的构造函数中,第二个参数应该是请求的 HTTP 方法,即 `"POST"` 或 `"GET"` 等。由于你的请求是 POST 请求,因此你需要将字符串 `"POST"` 作为第二个参数传递给构造函数。
3. 在发送请求之前,你需要设置请求的超时时间。可以使用 `request.timeout` 属性来设置超时时间,单位为秒。
4. 在请求头中,你设置了 `Content-Type` 为 `"application/json"`。如果你的请求数据不是 JSON 格式,那么你需要将 `Content-Type` 设置为相应的类型,例如 `"application/x-www-form-urlencoded"`。
下面是修改后的代码:
```
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using LitJson;
public class Example : MonoBehaviour
{
public UnityEngine.UI.Text resultText;
void Start()
{
StartCoroutine(PostRequest());
}
IEnumerator PostRequest()
{
string url = "your_post_url_here";
string postData = "your_post_data_here";
// Send POST request
UnityWebRequest request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(postData);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.timeout = 10; // Set request timeout to 10 seconds
yield return request.SendWebRequest();
// Parse response
if (request.result == UnityWebRequest.Result.Success)
{
string json = request.downloadHandler.text;
JsonData data = JsonMapper.ToObject(json);
string result = "Return Code: " + data["returnCode"] + "\n";
result += "Return Message: " + data["returnMessage"] + "\n";
result += "Data: " + data["data"] + "\n";
resultText.text = result;
}
else
{
Debug.Log(request.error);
}
}
}
```
using System.Collections; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using Newtonsoft.Json.Linq; using System.Collections.Generic; using System; public class KN_Data : MonoBehaviour { // Start is called before the first frame update public Dropdown dropdown; IEnumerator Start() { // 定义接口地址 string url = "http://158.58.50.21:8886/baseinfo/queryBaseInfo"; // 构造请求参数 var requestData = new { countryid = 3, areaid = 22, lineid = 27, metroid = 279, carriageid = "" }; 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 { string json = www.downloadHandler.text; // 解析json数据,获取下拉选项框的内容 List<string> options = new List<string>(); JObject jo = JObject.Parse(json); JArray ja = (JArray)jo["data"]; for(int i = 0; i < ja.Count; i++) { JObject item = (JObject)ja[i]; options.Add(item["carriagename"].ToString()); } // 将内容设置到下拉选项框中 Dropdown dropdown = GetComponent<Dropdown>(); dropdown.ClearOptions(); dropdown.AddOptions(options); } } }修改代码中的错误
在代码中,需要将以下这行代码:
```
string json = www.downloadHandler.text;
```
修改为:
```
string json = request.downloadHandler.text;
```
因为在代码中已经使用了 `UnityWebRequest` 对象 `request` 处理请求和返回结果,而非使用 `www` 对象。
阅读全文