Skip to content Skip to sidebar Skip to footer

Unity - Www.text Returning Null On Android Device

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 == NetworkReach

Solution 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);
    yieldreturn www;
    // check for errorsif (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.

Post a Comment for "Unity - Www.text Returning Null On Android Device"