Unity3D WWW发布数据异步 [英] Unity3D WWW Post data async
本文介绍了Unity3D WWW发布数据异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用WWW类将JSON发布到网站,但是我从服务器得到以下答案:同步问题".有没有一种方法可以从同步更改为异步?谢谢
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
推荐答案
您可以在协程中运行WWW作业(WWW很好地支持了这一点):
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);
}
}
}
这里有一个正在运行的项目,我用来与基于json的REST服务KiiCloud对话:
Here you have a running project which I use to talk to a json based REST service called KiiCloud:
HTH
这篇关于Unity3D WWW发布数据异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文