以下代码用于从我们自己的系统之一中托管的Thingworx服务器获取温度值。 这在团结中表现得很好 。 但是不在andoird中 ,一旦生成apk,它就不会从服务器中获取任何数据,并且会建立连接。但是,它只是不会获取数据并将其放入文本网格中。
我正在使用unity 5.4.1 32bit。检入Android-5.0.2和6。
using UnityEngine; using System.Collections; using UnityEngine.Networking; using System.Text.RegularExpressions; using System; using UnityEngine.UI; public class GETTempValue : MonoBehaviour { public GameObject TempText; static string TempValue; void Start() { StartCoroutine(GetText()); } IEnumerator GetText() { Debug.Log("Inside Coroutine"); while (true) { yield return new WaitForSeconds(5f); string url = "http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/"; Debug.Log("Before UnityWebRequest"); UnityWebRequest www = UnityWebRequest.Get (url); yield return www.Send(); Debug.Log("After UnityWebRequest"); if (www.isError) { Debug.Log ("Error while Receiving: "+www.error); } else { Debug.Log("Success. Received: "+www.downloadHandler.text); string result = www.downloadHandler.text; Char delimiter = '>'; String[] substrings = result.Split(delimiter); foreach (var substring in substrings) { if (substring.Contains ("</TD")) { String[] Substrings1 = substring.Split ('<'); Debug.Log (Substrings1[0].ToString()+"Temp Value"); TempValue = Substrings1 [0].ToString (); TempText.GetComponent<TextMesh> ().text = TempValue+"'C"; } } } } } }
这是android清单权限
uses-permission android:name="android.permission.INTERNET" uses-permission android:name="android.permission.CAMERA" uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
http://username:password@example.com出于安全原因,某些应用程序和操作系统不再支持将用户名和密码()嵌入url中,因为这不是执行HTTP身份验证的标准方法。Unity或Android很可能没有在自己这边实现。
http://username:password@example.com
我使用内置的Android浏览器对此进行了测试http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/,但无法正常运行。所以,我想这个问题来自Android。
http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/
我再次测试时没有用户名和密码,http://*IP Address**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/然后出现了登录窗口。当我输入用户名和密码时,它起作用了。
http://*IP Address**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/
您仍然可以使用UnityWebRequest通过提供解决这一问题AUTHORIZATION头到UnityWebRequest与SetRequestHeader功能。仅当授权类型Basic为时,此方法才有效Digest。您的情况是HTTP Basic。
UnityWebRequest
AUTHORIZATION
SetRequestHeader
Basic
Digest
HTTP Basic
对于一般解决方案:
string authenticate(string username, string password) { string auth = username + ":" + password; auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth)); auth = "Basic " + auth; return auth; } IEnumerator makeRequest() { string authorization = authenticate("YourUserName", "YourPassWord"); string url = "yourUrlWithoutUsernameAndPassword"; UnityWebRequest www = UnityWebRequest.Get(url); www.SetRequestHeader("AUTHORIZATION", authorization); yield return www.Send(); ....... }
对于您的问题的解决方案:
public GameObject TempText; static string TempValue; void Start() { StartCoroutine(GetText()); } string authenticate(string username, string password) { string auth = username + ":" + password; auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth)); auth = "Basic " + auth; return auth; } IEnumerator GetText() { WaitForSeconds waitTime = new WaitForSeconds(2f); //Do the memory allocation once string authorization = authenticate("Administrator", "ZZh7y6dn"); while (true) { yield return waitTime; string url = "http://*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/"; UnityWebRequest www = UnityWebRequest.Get(url); www.SetRequestHeader("AUTHORIZATION", authorization); yield return www.Send(); if (www.isError) { Debug.Log("Error while Receiving: " + www.error); } else { string result = www.downloadHandler.text; Char delimiter = '>'; String[] substrings = result.Split(delimiter); foreach (var substring in substrings) { if (substring.Contains("</TD")) { String[] Substrings1 = substring.Split('<'); Debug.Log(Substrings1[0].ToString() + "Temp Value"); TempValue = Substrings1[0].ToString(); TempText.GetComponent<TextMesh>().text = TempValue + "'C"; } } } } }