Unity - WWW.text 在 Android 设备上返回 null [英] Unity - WWW.text returning null on android device
问题描述
我正在使用 Unity 开发 Android 应用程序.但是我无法使用它连接到 Internet 服务器.这给出了错误,这很好:Application.internetReachability == NetworkReachability.NotReachable
I am developing an Android app with Unity. But I cannot connect to a internet server with it.
Tho this gives false, which is good: Application.internetReachability == NetworkReachability.NotReachable
但是当尝试执行此代码段时:
But when trying to execute this snippet:
IEnumerator testConnection() {
Dictionary<string, string> header = new Dictionary<string, string>();
string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
header.Add("User-Agent", userAgent);
WWW www = new WWW("www.google.com", null, header);
yield return www;
// check for errors
if (www.error == null) {
util.debug("works");
} else {
// www.error and www.text both are empty
util.debug("testing: WWW Error: " + www.error + www.text);
}
}
它通过统一编辑器和 Windows 可执行文件工作,但不适用于我的 android 设备 (v 6)有没有已知的解决方案?
it works via unity editor and windows executable, but not on my android device (v 6) Is there a known solution to this?
Ping 似乎也有效:
Ping also seem to work:
IEnumerator PingGoogle() {
Ping googPing = new Ping("172.217.6.195");
while (!googPing.isDone) {
yield return googPing;
}
util.debug("ping works: " + googPing.time); //I reach this point with the app
}
所以我认为 WWW
类有问题?
So i think something is wrong with the WWW
-class?
安卓版本:6.0.1
OxygenOS 版本:3.5.6
OxygenOS-Version: 3.5.6
Unity 版本:5.6.0b3 个人版(64 位)
Unity version: 5.6.0b3 Personal (64bit)
我将 Internet Access
的 PlayerSettings(据我所知,这是 android 清单)从 Auto
更改为 Require
.没有成功
I changed the PlayerSettings (which is the android manifest as far as i can tell) of Internet Access
from Auto
to Require
. No success
编辑 2:看起来 www.error
根本不是空的.消息刚刚被截断,因为它对于 unitys-textelement 来说太长了(我的错).错误是 java.net.MalformedURLException: Protocol not found: www.google.de
.所以唯一缺少的是协议,即:http://
.当我从评论中尝试建议的解决方案时,我发现了这个问题.
It appears that www.error
wasnt empty at all. The Message just got truncated because it was too long for unitys-textelement (my fault). The error was java.net.MalformedURLException: Protocol not found: www.google.de
.
So the only thing that was missing was the protocoll, i.e.: http://
. I found this problem when i tried a suggested solution from the comments.
推荐答案
我对您修改后的代码进行了快速测试,但仍然无法正常工作,并出现此运行时异常:
I ran a quick test with your modified code that is still not working and got this run-time exception:
java.net.MalformedURLException:未找到协议
java.net.MalformedURLException: Protocol not found
遇到此类问题时使用 Android Monitor 总是好的.
It's always good to use Android Monitor when having such problems like this.
问题是您没有在 url 前面加上 http://
或 https://
前缀.Android 不支持它,所以这就是它在编辑器上工作但在 Android 上不工作的原因.
The problem is that you did not prefix the url with http://
or https://
. Android does not support that so that's why it worked on the Editor but not on Android.
当您尝试在 url 中嵌入用户名和密码时,也会发生同样的事情.例如,http://username:password@example.com.
The-same thing happens also happens when you try to embed user name and password in a url. For example, http://username:password@example.com.
这适用于 Windows 和编辑器,但不能适用于 Android,但有一个修复 为它.
This will work on Windows and the Editor but will not work on Android but there is a fix for it.
这应该有效:
IEnumerator testConnection()
{
Dictionary<string, string> header = new Dictionary<string, string>();
string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
header.Add("User-Agent", userAgent);
WWW www = new WWW("http://www.google.com", null, header);
yield return www;
// check for errors
if (www.error == null)
{
util.debug("works");
}
else
{
// www.error and www.text both are empty
util.debug("testing: WWW Error: " + www.error + www.text);
}
}
提示:
当从 Unity 应用程序向不属于您的服务器发出网络请求时 (http://www.google.com),添加 user-agent 标头总是一个好主意,否则当您的应用发布时,请求会在某些设备上失败.
When making a web request from Unity app to a server that does not belong to you (http://www.google.com), it is always a good idea to add user-agent header or expect the request to fail on some devices when your app is released.
这篇关于Unity - WWW.text 在 Android 设备上返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!