Guayavas
Guayavas

Reputation: 21

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

}

Upvotes: 0

Views: 3367

Answers (2)

Codemaker2015
Codemaker2015

Reputation: 15659

  • If you are running this code on your mobile device then you should change the hostname. You can't use localhost in the edge device. So, instead of localhost you have to put the IP address of the computer where the backend server is running.

    For getting the IP address,

    Take command prompt/terminal then type the following command,

    For Windows machine

    ipconfig
    

    For Linux / Mac machine

    ifconfig
    

    Then take the IPv4 address and replace the localhost with this IP like this,

    using (UnityWebRequest webRequest = UnityWebRequest.Post("http://192.168.1.9:3000/login", form))
    

    enter image description here

  • You must ensure that you have connected the computer (in which the server is running) and the mobile device to the same network. You can do it by simply connecting all devices via wifi network.

  • If you are using a mobile device that is running on Android 9, Pie (API level 27+) or later version of the operating system then you have to specify the android:usesCleartextTraffic="true" in the AndroidManifest.xml file.

    So, Click on Player Settings --> Publishing Settings --> Check custom manifest template. You can add the android:usesCleartextTraffic="true" in the manifest file like this,

    <?xml version="1.0" encoding="utf-8"?>
    <manifest ...>
        <uses-permission android:name="android.permission.INTERNET" />
        <application
            ...
            android:usesCleartextTraffic="true"
        ...>
        ...
        </application>
    </manifest>
    

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
        <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
        <uses-permission android:name="android.permission.INTERNET" />
        <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true" android:usesCleartextTraffic="true">
            <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name">
                <intent-filter>
                     <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
           </activity>
      </application>
    </manifest>
    

Upvotes: 0

derHugo
derHugo

Reputation: 90813

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.

Upvotes: 1

Related Questions