2012-11-21 11 views
2

나는 화합의 초보자입니다.wwwform을 사용하여 json을 단일성으로 전달하는 방법

내가

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

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

나는 C#을

을 사용하고 이

사람이 나에게 이것에 대한 솔루션을 제공 할 수 있습니다 단결의 다음 JSON 데이터를 갖는 POST 요청을 보낼

?

+1

유니티 태그는 Microsoft Unity 용입니다. 오용하지 마십시오. –

답변

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

} 

희망을! =)

1

아래에서이 작업을 수행했습니다. 가자 : ==>

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); } 
} 
} 
관련 문제