Script does nothing when published to Android

I’m using the following script on a UI button in Unity and it works great in the editor. It even works when published to PC/MAC…however this script does nothing when published to Android:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

class openWebPage: MonoBehaviour {
    void Start() {
        StartCoroutine(GetText());
    }
    IEnumerator GetText() {
        UnityWebRequest www = UnityWebRequest.Get("http://myUsername:myPassword@my.ip.address.here/myCommand");
        yield return www.Send();
        if(www.isError) {
            Debug.Log(www.error);
        }
        else {
            // Show results as text
            Debug.Log(www.downloadHandler.text);
            // Or retrieve results as binary data
            byte[] results = www.downloadHandler.data;
        }
    }
}

Why doesn’t it function on Android?

Use log viewer Unity Asset Store - The Best Assets for Game Making to see the console when built to android and see if you are getting any errors.

1 Like

I know iOS has additional security for web requests that must be turned off or worked around before you can get stuff from the web; Android may have something similar.

Do you have any error messages in your Android debug console? (This question may be better suited to the Android subforum.)

1 Like

Thank you for the response. Yes, I get 1 message in the Android debug console:
“Game scripts or other custom code contains OnMouse_event handlers. Presence of such handlers might impact performance on handheld devices. UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()”

However, even when I disable the OnMouse_event handler and use Touch Events instead, the script still does nothing.

After running several tests I found it has nothing to do with my mouse event or touch event because I put a DestroySelf script on my button and when I press the button it destroys itself (even on mobile). So the event is firing, but the script is not doing anything on mobile. Maybe C# doesn’t work on Android? I guess I can try re-writing it as Javascript and see if it works?

No, c# works fine on Android. Considering it’s not like it’s c# once it’s done being built to Android. There are not thousands of Android developers in Unity learning Unityscript just to do builds to it.

So Log viewer didn’t give you your debug messages? Or are you using logcat?

Since you have two debugs there, one or the other should show up if the call completes correctly. Maybe www.isError isn’t correct. Not sure, but UnityWebRequest docs show a using statement(to dispose of it when done) and two other types of error checks
https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Get.html

Maybe try switching out your error if for the example they use and see if it makes a difference.

1 Like

I’ve figured out the solution! Mobile devices don’t allow you to send an http request with a username and a password in it (for security purposes). Instead, you have to pass an authentication string and then send the http request. Here’s my code that finally worked on Android:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class OpenWebPage: MonoBehaviour {
            void Start()
            {
                StartCoroutine(GetText());
            }

            string authenticate(string username, string password)
            {
                string auth = username + ":" + password;
                auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
                auth = "Basic " + auth;
                return auth;
            }

            IEnumerator GetText()
            {

                string authorization = authenticate("InsertUserNameHere", "InsertPasswordHere");
                while (true)
                {
                    string url = "http://insertWebAddressHere/optionalCommand";


                    UnityWebRequest www = UnityWebRequest.Get(url);
                    www.SetRequestHeader("AUTHORIZATION", authorization);
                    yield return www.Send();

        }
    }
}

Remember to change the name of the public class to be the same name as your script. In my case, I called my script OpenWebPage so I named my class OpenWebPage.