Unity - WWW.text returning null on android device

2019-02-25 03:52发布

问题:

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); 
    }
}

it works via unity editor and windows executable, but not on my android device (v 6) Is there a known solution to this?

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
}

So i think something is wrong with the WWW-class?

Android version: 6.0.1

OxygenOS-Version: 3.5.6

Unity version: 5.6.0b3 Personal (64bit)

Edit: 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: 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.

回答1:

I ran a quick test with your modified code that is still not working and got this run-time exception:

java.net.MalformedURLException: Protocol not found

It's always good to use Android Monitor when having such problems like this.

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.

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.

This will work on Windows and the Editor but will not work on Android but there is a fix for it.

This should work:

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);
    }
}

Hint:

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.