2016-06-29 14 views
0

Unityで安らかなWeb APIへのPOSTリクエストを作成しようとしています。Unity:JSONを使用したWWWクラスを使用したPOSTリクエスト

ヘッダがContent-Type: application/json

データがキーとJSON文字列が値であるところの生データ入力の例は、ある次のようになります。

{ 
    "data":{ 
     "username":"name", 
     "email":"[email protected]", 
     "age_range":21, 
     "gender":"male", 
     "location":"california" 
    } 
} 

は、ここに私のスクリプトです:

private static readonly string POSTAddUserURL = "http://db.url.com/api/addUser"; 
public WWW POST() 
{ 
    WWW www; 
    Hashtable postHeader = new Hashtable(); 
    postHeader.Add("Content-Type", "application/json"); 
    WWWForm form = new WWWForm(); 
    form.AddField("data", jsonStr); 
    www = new WWW(POSTAddUserURL, form); 
    StartCoroutine(WaitForRequest(www)); 
    return www; 
} 

IEnumerator WaitForRequest(WWW data) 
{ 
    yield return data; // Wait until the download is done 
    if (data.error != null) 
    { 
     MainUI.ShowDebug("There was an error sending request: " + data.error); 
    } 
    else 
    { 
     MainUI.ShowDebug("WWW Request: " + data.text); 
    } 
} 

フォームとヘッダーの両方でWWWクラスを使用してリクエストを送信するにはどうすればよいですか?または、一般的には、このような投稿要求をどのように送信すればよいですか?

答えて

4

あなたは生のJSONデータを追加したい場合は、ちょうど私が下記これを行うためにやったWWWForm

public WWW POST() 
{ 
    WWW www; 
    Hashtable postHeader = new Hashtable(); 
    postHeader.Add("Content-Type", "application/json"); 

    // convert json string to byte 
    var formData = System.Text.Encoding.UTF8.GetBytes(jsonStr); 

    www = new WWW(POSTAddUserURL, formData, postHeader); 
    StartCoroutine(WaitForRequest(www)); 
    return www; 
} 
-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); } 
} 

} 
0
try { 
    string url_registerEvent = "http://demo....?parameter1=" parameter1value"&parameter2="parameter2value; 

    WebRequest req = WebRequest.Create (url_registerEvent); 
    req.ContentType = "application/json"; 
    req.Method = "SET"; 
    //req.Credentials = new NetworkCredential ("[email protected]", "connect10api"); 
    HttpWebResponse resp = req.GetResponse() as HttpWebResponse; 

    var encoding = resp.CharacterSet == "" 
       ? Encoding.UTF8 
       : Encoding.GetEncoding (resp.CharacterSet); 

    using (var stream = resp.GetResponseStream()) { 
     var reader = new StreamReader (stream, encoding); 
     var responseString = reader.ReadToEnd(); 

     Debug.Log ("Result :" + responseString); 
     //JObject json = JObject.Parse(str); 
    } 
} catch (Exception e) { 
    Debug.Log ("ERROR : " + e.Message); 
} 
関連する問題