0

WWW クラスを使用して JSON を Web サイトに投稿したいのですが、サーバーから「同期の問題」という回答が返ってきました。同期から非同期に変更する方法はありますか? ありがとうございました

4

2 に答える 2

0

ドイツ語からの回答は非常に役に立ちましたが、コンパイルして実行できるようにいくつかの調整を行いました (サンプルのシリアライゼーション/デシリアライゼーション ビットを使用)。

投稿先の 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);
        }    
    }    
}
于 2016-03-22T14:57:55.553 に答える