2017-06-06 52 views
0
using UnityEngine; 
using System.Collections; 
using System.Text; 

public class Network : MonoBehaviour 
{ 
    // Use this for initialization 
    void Start() 
    { 
     Debug.Log("[waitforComm] Hello, World!"); 

     string postData = "{\"waitforCommData\":1}"; 
     Hashtable headers = new Hashtable(); 
     headers.Add("Content-Type", "application/json"); 

     byte[] pData = Encoding.ASCII.GetBytes(postData.ToCharArray()); 

     WWW www = new WWW("http://localhost:8080/jspsample/process.jsp", pData, headers); 

     Debug.Log("[waitforComm] post message requested."); 

     StartCoroutine(waitforRequest(www)); 
    } 

    // Update is called once per frame 
    void Update() 
    { } 

    private IEnumerator waitforRequest(WWW www) 
    { 
     yield return www; 
     Debug.Log("[waitforComm] response : " + www.text); 
    } 
} 

這是我們的代碼,我不能弄清楚該部分統一連接到網絡由JSP

萬維網WWW =新萬維網( 「http://localhost:8080/jspsample/process.jsp」 的pData,標頭);

我們的錯誤在於 參數3頭:無法從「System.Collections.Hashtable」轉換爲「System.Collections.Generic.Dictionary」

+1

它曾經是'Hashtable',並且該代碼曾經有效,但隨後Unity改變了參數以在新版本中使用'Dictionary'。 [Merk的](https://stackoverflow.com/a/44381316/3785314)答案應該解決您的問題。 – Programmer

+0

@ J.Choi - 如果任何答案解決了您的問題,請接受這個答案,以幫助他人找到直接答案並節省他們的時間:) –

回答

2

Unity documentation for the WWW object constructor this code is usin克是很清楚,但不結晶。

第三個參數被聲明爲Dictionary<string, string>,即使文本顯示爲「散列表」。

所以不是:

Hashtable headers = new Hashtable(); 
headers.Add("Content-Type", "application/json"); 

務必:

var headers = new Dictionary<string, string>(); 
headers.Add("Content-Type", "application/json"); 

,你應該是好去。

+0

它修好了。讚賞它! –

+0

@ J.Choi很高興幫助。當你有一刻時請接受答案。 –