Www.text on Android device returned null

I'm using unity to develop an Android application. But I can't use it to connect to the Internet server. This gives an error, which is very good: application. Internetreachability = = networkreachability. Notreachable

However, 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 through a unified editor and windows executables, but it doesn't work on my Android device (v 6). Do you have a known solution?

Ping also seems 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 there is a problem with the WWW class?

Android version: 6.0.1

Oxygenos version: 3.5.6

Unity version: 5.6.0b3 personal version (64 bit)

Edit: I changed the playersettings (Android list as far as I know) of Internet access from "automatic" to "required". It didn't succeed

Editor 2: it seems that www.error is not empty at all. The message has just been truncated because it is too long to be unified (my fault). The error is java.net.malformedurlexception: the protocol cannot be found: www.google.de. Therefore, the only missing protocol is http: / /. I found this problem when I tried the proposed solution from the comments

resolvent:

I quickly tested the modified code and found the following runtime exceptions:

In case of such problems, it is best to use Android monitor

The problem is that you didn't prefix the URL with http: / / or HTTPS: / /. Android doesn't support this feature, which is why it can run on editor but can't run on Android

The same happens when you try to embed the user name and password into the URL. For example http://username:password @example.com.

This will apply to windows and editors, but not Android, but fix

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 a web request is made from the unity application to a server that does not belong to you( http://www.google.com ), it is best to add user agent headers or expect requests on some devices to fail after the application is published

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>