【问题标题】:how to pass json with wwwform in unity如何统一传递带有wwwform的json
【发布时间】:2012-11-21 11:15:53
【问题描述】:

我是团结的新手。

我想统一发送一个包含以下 json 数据的 post 请求

**url** = "http://index.php"

**sampple json** = {"id":"100","name":"abc"}

我正在使用 C#

谁能为此提供解决方案?

【问题讨论】:

  • 统一标签适用于 Microsoft Unity。请不要滥用它。

标签: c# unity3d


【解决方案1】:

嗯,我正在处理这样的事情:

    public class RequestConnectionManager : Manager<RequestConnectionManager>
    {

    public int maxSubmissionAttempts = 3;

    public Coroutine post() {
                WWWForm playForm = new WWWForm();
                playForm.AddField("id", myJson.id);
                playForm.AddField("name", myJson.name);

                Post playPost = new Post("http://index.php", playForm, maxSubmissionAttempts, this);
                return StartCoroutine(PostWorker(playPost));
            }

    private IEnumerator PostWorker(Post playPost)
        {
            yield return null;
            yield return playPost.Submit();

            Debug.Log(playPost.Response);
            if (playPost.Error != null)
            {
                MessageBoxManager.Instance.Show("Error: " + playPost.Error, "Error", MessageBoxManager.OKCancelOptionLabels, MessageOptions.Ok);
            }
            else
            {
                try
                {
                    //do whatever you want in here
                    //Hashtable response = JsonReader.Deserialize<Hashtable>(playPost.Response);
                    //Debug.Log("UNITY LOG..." + response);

                }
                catch (JsonDeserializationException jsExc)
                {
                    Debug.Log(jsExc.Message);
                    Debug.Log(playPost.Response);
                }
                catch (Exception exc)
                {
                    Debug.Log(exc.Message);
                    Debug.Log(playPost.Response);
                }

            }
        }

    }

//As for the Manager class...

using UnityEngine;
using System.Collections;


// I wonder what the constraint where TManager : Singleton<TManager> would produce...
public class Manager<TManager> : SingletonW<TManager> where TManager : MonoBehaviour
{

    override protected void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this);
        DontDestroyOnLoad(gameObject);
    }

}

希望这会有所帮助! =)

【讨论】:

    【解决方案2】:

    我已经在下面这样做了。走吧:==>

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using System.Collections.Generic;
    public class btnGetData : MonoBehaviour {
     void Start()
     {
         gameObject.GetComponent<Button>().onClick.AddListener(TaskOnClick);
     }
     IEnumerator WaitForWWW(WWW www)
     {
         yield return www;
    
    
         string txt = "";
         if (string.IsNullOrEmpty(www.error))
             txt = www.text;  //text of success
         else
             txt = www.error;  //error
         GameObject.Find("Txtdemo").GetComponent<Text>().text =  "++++++\n\n" + txt;
     }
     void TaskOnClick()
     {
         try
         {
             GameObject.Find("Txtdemo").GetComponent<Text>().text = "starting..";   
             string ourPostData = "{\"plan\":\"TESTA02\"";
             Dictionary<string,string> headers = new Dictionary<string, string>();
             headers.Add("Content-Type", "application/json");
             //byte[] b = System.Text.Encoding.UTF8.GetBytes();
             byte[] pData = System.Text.Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
             ///POST by IIS hosting...
             WWW api = new WWW("http://192.168.1.120/si_aoi/api/total", pData, headers);
             ///GET by IIS hosting...
             ///WWW api = new WWW("http://192.168.1.120/si_aoi/api/total?dynamix={\"plan\":\"TESTA02\"");
             StartCoroutine(WaitForWWW(api));
         }
         catch (UnityException ex) { Debug.Log(ex.Message); }
     } 
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2017-10-25
      • 2013-04-29
      相关资源
      最近更新 更多