C# 统一发送POST请求
我试图在Unity中发送一个POST请求,但是我不能这样做,因为应该这样做的函数被跳过了,我不知道为什么。 我添加了日志以了解发生了什么,并且在日志中出现了名为“永不”的“(post-json)文本,尽管该文本表示已通过函数((发送已玩的游戏):postjson之后)C# 统一发送POST请求,c#,unity3d,C#,Unity3d,我试图在Unity中发送一个POST请求,但是我不能这样做,因为应该这样做的函数被跳过了,我不知道为什么。 我添加了日志以了解发生了什么,并且在日志中出现了名为“永不”的“(post-json)文本,尽管该文本表示已通过函数((发送已玩的游戏):postjson之后) private static void SendGamePlayed() { int incrementedGamePlayed = GetGamePlayed () + 1; EventObject anEven
private static void SendGamePlayed() {
int incrementedGamePlayed = GetGamePlayed () + 1;
EventObject anEvent = CreateEvent(EVENT_GAME_PLAYED, incrementedGamePlayed, null, null);
AbcEvent aAbcEvent = new AbcEvent (bundleID, appToken, anEvent);
string json = JsonUtility.ToJson (aAbcEvent);
// test
Debug.Log("(send game played): " + json);
PostJSON (json, EVENT_ROUTE);
Debug.Log ("(send game played): after postjson");
SetGamePlayed (incrementedGamePlayed);
}
private static IEnumerator PostJSON(string jsonString, string route) {
// test
Debug.Log("(post json) called");
string url = SERVER_URL + route;
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
Debug.Log ("(post json) before sending");
yield return request.Send();
Debug.Log ("(post json) after sending");
if (request.error != null) {
Debug.Log("request error: " + request.error);
} else {
Debug.Log("request success: " + request.downloadHandler.text);
}
}
谢谢你的帮助 您正在使用Unity的功能进行异步操作。要理解这些概念,请阅读和。要运行函数,请按如下方式调用
startcroutine
函数:
StartCoroutine(PostJSON (json, EVENT_ROUTE));
编辑:
更具体地说:您正在创建一个调用异步函数(Send)的(PostJson)。是的..这正是所发生的事情。我不会称之为“异步”,但您的观点是正确的。
UnityWebRequest.Send
函数是异步的。不是“并发的”,而是真正的“异步的”=)