Search code examples
unity-game-enginewebrequestunitywebrequest

unknown error with the unitywebrequest function


I'm doing an apk which when I run it on my cell phone gives me the following error

"Unknow error"

but the strangest thing is that if it works normally when I run it from unity, I used the following code to show me what the error was when I executed it on my cell phone because in unity works perfectly

IEnumerator logIn(WWWForm form)
{
    using (UnityWebRequest webRequest = UnityWebRequest.Post("http://localhost:3000/login", form))
    {
        yield return webRequest.SendWebRequest();

        if (webRequest.isNetworkError )
        {
            Debug.Log(webRequest.error);
            advertencia.SetActive(true);
            advertencia.GetComponent<Text>().text=webRequest.error+"1";
        }
        else if (webRequest.isHttpError)
        {
            advertencia.SetActive(true);
            advertencia.SetActive(true);
            advertencia.GetComponent<Text>().text = webRequest.error+"2";
        }
        else
        {                
            SceneManager.LoadScene("Principal");
        }
    }
}

check if my apk was connected to the internet with the following code that shows a text if it connects to the internet

private void Update()
{
    if (Application.internetReachability == NetworkReachability.NotReachable)
    {
        advertencia.SetActive(true);
        Debug.Log("Error. Check internet connection!");
    }

}

The code a little more complete:

private Text userText;
private InputField password;
public GameObject advertencia;

    private void Start()
{
    userText = GameObject.Find("UserInput").GetComponent<Text>();
    password = GameObject.Find("PasswordInput").GetComponent<InputField>();
    advertencia = GameObject.Find("Advertencia");
    advertencia.SetActive(false);

}

  //the function with which the corrutina invoked
   public void Log()
{
    Debug.Log("Usuario : " + userText.text + "\nContraseña : " + password.text);

    WWWForm form = new WWWForm();
    form.AddField("codigo", userText.text);
    form.AddField("contrasena", password.text);

    StartCoroutine(logIn(form));

}

Solution

  • Problem

    You use the URL

    http://localhost:3000/login
    

    You are trying to send the web request to the host localhost. This works on your PC in Unity since the PC is the server you a trying to contact.

    The server isn't running on your phone but your phone is trying to send the request to itself at port 3000 which will fail obviously.

    Read more on What is a localhost?

    Solution

    Replace the localhost by the IP or network address your server/PC actually has and you should be fine.

    To find out your PC's IP use e.g.

    • Linux/Unix: In a terminal call ifconfig
    • Windows: In the CMD call ipconfig

    Ofcourse your PC and phone then also have to be in the same network or at least be routed so your phone can reach the server/PC at the given IP/network address.

    Possible that you also have to configure your PC's firewall to allow incoming traffic on that port.