Unity3D WWW Post data async

2019-08-25 06:08发布

I want to post a JSON to a website using the WWW class, But I get this answer from the server: "Synchronization problem.". Is there a way to change from sync to async? Thank You

标签: unity3d
2条回答
手持菜刀,她持情操
2楼-- · 2019-08-25 06:51

The answer from German was very helpful, but I made some tweaks so that it'll compile and run (with sample serialization / deserialization bits).

Just pass in the BaseUrl you want to post to, i.e. http://www.domain.com/somecontroller/someaction or whatever.

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);
        }    
    }    
}
查看更多
看我几分像从前
3楼-- · 2019-08-25 07:04

You can run your WWW job in a coroutine (WWW supports this well):

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);
        }    
    }    
}

Here you have a running project which I use to talk to a json based REST service called KiiCloud:

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

HTH

查看更多
登录 后发表回答