【问题标题】:Unity3D WWW Post data asyncUnity3D WWW 发布数据异步
【发布时间】:2013-03-09 14:57:59
【问题描述】:

我想使用 WWW 类将 JSON 发布到网站,但我从服务器得到这个答案:“同步问题。”。有没有办法从同步更改为异步?谢谢你

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    您可以在协程中运行您的 WWW 作业(WWW 很好地支持这一点):

    using UnityEngine;
    
    public class PostJSON : MonoBehaviour {
    
        void Start () {
    
            string url = "http://your_url_endpoint";
    
            WWWForm form = new WWWForm();
            Hashtable headers = form.headers;
            headers["Content-Type"] = "application/json";
            Hashtable data = new Hashtable();
            data["message"] = "a sample message sent to service as json";
            string json = JSON.JsonEncode(data);
    
            byte[] bytes = Encoding.UTF8.GetBytes(json);
            WWW www = new WWW(url, bytes, headers);
    
            StartCoroutine(WaitForRequest(www));
        }
    
        IEnumerator WaitForRequest(WWW www)
        {
            yield return www
    
            // check for errors
            if (www.error == null)
            {
                Debug.Log("WWW Ok!: " + www.data);
            } else {
                Debug.Log("WWW Error: "+ www.error);
            }    
        }    
    }
    

    这里有一个正在运行的项目,我用它来与一个名为 KiiCloud 的基于 json 的 REST 服务通信:

    http://blog.kii.com/?p=2939

    HTH

    【讨论】:

    • 尝试第三方库,如 SimpleJSON 或 MiniJSON
    • 我使用了 5.3 统一。它已经包含 jsonutility 现在代码工作和输出是 {name :"dfdfdfsfsfsfblahblah"} 并且在服务器上找不到数据
    • 仔细检查您的请求,特别是标头。查看是否收到错误“WWW 错误:”如果您设法构建了 JSON 对象,那么您现在的请求本身就有问题(可能服务器需要不同的内容类型、更多参数或某种身份验证令牌)跨度>
    【解决方案2】:

    来自德语的回答非常有帮助,但我做了一些调整,以便它能够编译和运行(带有示例序列化/反序列化位)。

    只需传入您要发布到的 BaseUrl,即 http://www.domain.com/somecontroller/someaction 或其他。

    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    
    using UnityEngine;
    
    [Serializable]
    public class Person
    {
        public string Name;
    }
    
    [Serializable]
    public class Response
    {
        public string SomeValue;
    }
    
    public class PostJSON : MonoBehaviour
    {
        public string BaseUrl;
    
        private WWWForm form;
        private Dictionary<string, string> headers = null;
    
        void Start () 
        {
            var basUrlNotSpecified = string.IsNullOrEmpty(BaseUrl);
    
            if(basUrlNotSpecified) 
            {
                Debug.LogWarning("BaseUrl value not specified. Post abandoned.");
                return;
            }
    
            form = new WWWForm();
            headers = form.headers;
            headers["Content-Type"] = "application/json";
            headers["Accept"] = "application/json";
    
            var person = new Person
            {
                Name = "Iulian Palade"
            };
    
            var json = JsonUtility.ToJson(person);
    
            byte[] bytes = Encoding.UTF8.GetBytes(json);
    
            WWW www = new WWW(BaseUrl, bytes, headers);
    
            StartCoroutine(WaitForRequest(www));
        }
    
        IEnumerator WaitForRequest(WWW www)
        {
            yield return www;
    
            if (www.error == null)
            {
                Debug.Log("WWW Ok!: " + www.text);
                var response = JsonUtility.FromJson<Response>(www.text);
                Debug.Log(response.SomeValue);
            } 
            else 
            {
                Debug.Log("WWW Error: "+ www.error);
            }    
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 2019-02-20
      相关资源
      最近更新 更多