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); } } }修改代码中的错误
时间: 2024-02-10 12:15:55 浏览: 67
详解java Collections.sort的两种用法
在代码中,需要将以下这行代码:
```
string json = www.downloadHandler.text;
```
修改为:
```
string json = request.downloadHandler.text;
```
因为在代码中已经使用了 `UnityWebRequest` 对象 `request` 处理请求和返回结果,而非使用 `www` 对象。
阅读全文