我是团结的新手。
我想统一发送一个包含以下 json 数据的 post 请求
**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);
}
}
希望这可以帮助!=)
我已经在下面这样做了。我们走吧:==>
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); }
}
}