以下代码用于从我们自己的系统中托管的Thingworx服务器获取温度值。 这在团结中非常有效。 但不是在andoird ,一旦生成了apk,它就不会从服务器获取任何数据,并且会建立连接。但是,它只是不会获取数据并将其放入文本网格中。
我使用统一5.4.1 32位。检查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"
答案 0 :(得分:10)
出于安全原因,某些应用程序和操作系统不再支持在网址中嵌入用户名和密码(http://username:password@example.com
)。这是因为这不是执行HTTP身份验证的标准方法。很可能Unity或Android没有实现这一点。
我使用http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/
在内置Android浏览器上对此进行了测试,但无法正常运行。所以,我猜这个问题来自Android。
我在没有用户名和密码http://*IP Address**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/
的情况下再次测试,然后出现了登录窗口。当我输入用户名和密码时,它有效。
您仍然可以使用UnityWebRequest
来解决此问题,方法是使用AUTHORIZATION
函数向UnityWebRequest
提供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";
}
}
}
}
}