2017-06-06 49 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很高兴帮助。当你有一刻时请接受答案。 –