在Unity

时间:2017-05-12 11:40:57

标签: c# unity3d

我试图在Unity中发送POST请求,但我不能这样做因为应该跳过的功能被忽略了,我不知道为什么。 我已经添加了日志以了解发生了什么,并且"(发布json)调用"永远不会出现,即使文字说已超过功能(发送游戏):postjson之后出现在日志中。

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

    }
}

感谢您的帮助!

0 个答案:

没有答案