Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在unity中使用wwwform传递json_C#_Unity3d - Fatal编程技术网

C# 如何在unity中使用wwwform传递json

C# 如何在unity中使用wwwform传递json,c#,unity3d,C#,Unity3d,我是unity的新手 我想发送一个post请求,其中包含unity中的以下json数据 **url** = "http://index.php" **sampple json** = {"id":"100","name":"abc"} 我用的是C# 有人能为我提供一个解决方案吗?好吧,我一直在做这样的事情: public class RequestConnectionManager : Manager<RequestConnectionManager> {

我是unity的新手

我想发送一个post请求,其中包含unity中的以下json数据

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

**sampple json** = {"id":"100","name":"abc"}
我用的是C#


有人能为我提供一个解决方案吗?

好吧,我一直在做这样的事情:

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

}
公共类请求连接管理器:管理器
{
public int maxSubmissionAttempts=3;
公共协同程序post(){
WWWForm playForm=新WWWForm();
AddField(“id”,myJson.id);
AddField(“name”,myJson.name);
Post playPost=新帖子(“http://index.php“、playForm、MaxSubmission、this);
返回Start例程(PostWorker(playPost));
}
私人IEnumerator邮局工作人员(Post playPost)
{
收益返回空;
返回playPost.Submit();
Debug.Log(playPost.Response);
if(playPost.Error!=null)
{
MessageBoxManager.Instance.Show(“错误:”+playPost.Error,“Error”,MessageBoxManager.Ok取消选项标签,MessageOptions.Ok);
}
其他的
{
尝试
{
//在这里你想干什么就干什么
//Hashtable response=JsonReader.Deserialize(playPost.response);
//Debug.Log(“统一日志…”+响应);
}
捕获(JsonDeserializationException jsExc)
{
Log(jsExc.Message);
Debug.Log(playPost.Response);
}
捕获(异常exc)
{
Debug.Log(exc.Message);
Debug.Log(playPost.Response);
}
}
}
}
//至于经理班。。。
使用UnityEngine;
使用系统集合;
//我想知道TManager:Singleton会产生什么约束。。。
公共类管理器:SingletonW,其中TManager:MonoBehavior
{
重写受保护的void Awake()
{
base.Awake();
DontDestroyOnLoad(此);
DontDestroyOnLoad(游戏对象);
}
}

希望这有帮助!=)

我在下面做了这件事。我们走吧:==>

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); }
 } 
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
使用System.Collections.Generic;
公共类btnGetData:单一行为{
void Start()
{
gameObject.GetComponent().onClick.AddListener(TaskOnClick);
}
IEnumerator WaitForWWW(WWW)
{
收益率;
字符串txt=“”;
if(string.IsNullOrEmpty(www.error))
txt=www.text;//成功文本
其他的
txt=www.error;//错误
GameObject.Find(“Txtdemo”).GetComponent().text=“++++++++++\n\n”+txt;
}
void TaskOnClick()
{
尝试
{
GameObject.Find(“Txtdemo”).GetComponent().text=“start..”;
字符串ourPostData=“{\”计划“:\”测试02\”;
字典头=新字典();
添加(“内容类型”、“应用程序/json”);
//byte[]b=System.Text.Encoding.UTF8.GetBytes();
byte[]pData=System.Text.Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
///由IIS宿主发布。。。
WWW-api=新的WWW(“http://192.168.1.120/si_aoi/api/total“、pData、标题);
///通过IIS主机获取。。。
///WWW-api=新的WWW(“http://192.168.1.120/si_aoi/api/total?dynamix={“计划”:“测试02”);
start例程(WaitForWWW(api));
}
catch(UnityException ex){Debug.Log(ex.Message);}
} 
}

unity标签适用于Microsoft unity。请不要误用。