Unity-WWW.text在Android设备上返回null [英] Unity - WWW.text returning null on android device

查看:332
本文介绍了Unity-WWW.text在Android设备上返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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?

Android版本:6.0.1

Android version: 6.0.1

OxygenOS版本:3.5.6

OxygenOS-Version: 3.5.6

统一版本: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

Edit2: 看来www.error根本不是空的.该消息刚刚被截断,因为它太长了,无法统一(我的错).错误为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不支持该功能,这就是为什么它可以在Editor上运行,而不在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆