Search Unity

Resolved UnityWebRequest Returns "Unknown Error" on Android Device

Discussion in 'Multiplayer' started by ozlgktg, May 11, 2020.

  1. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Dear Forum Members,

    I send a "mail" variable to PHP page. This PHP page finds this "mail" variable on my DB(MySql) and returns ID number of this email. Its working perfect when I run it on Unity.

    But when i build it for Android (.apk) its not working on phone device. And it returns "404 not found!". I mean the page is not found. Why is this problem exist only Android device? The PHP url is working on Unity. And it returns ID number of "mail" variable from DB.

    Thank you all for help.

    EDIT-1: I changed my error handler to "w.error". And now it returns "Unknown Error" on Android device...

    My Server.cs:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5.  
    6. public class Server : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField] InputField Mail;
    10.     [SerializeField] Text errorMessages;
    11.     [SerializeField] GameObject progressCircle;
    12.     [SerializeField] Button loginButton;
    13.     [SerializeField] string url;
    14.     WWWForm form;
    15.  
    16.  
    17.  
    18.     public void ServerBaslat ()
    19.     {
    20.      
    21.         loginButton.interactable = false;
    22.         progressCircle.SetActive (true);
    23.         StartCoroutine(Login());
    24.     }
    25.  
    26.  
    27.     IEnumerator Login ()
    28.     {
    29.         form = new WWWForm ();
    30.      
    31.         form.AddField ("mail", Mail.text);
    32.         UnityWebRequest w = UnityWebRequest.Post (url, form);
    33.         yield return w.SendWebRequest();
    34.      
    35.  
    36.         if (w.isNetworkError || w.isHttpError) {
    37.             errorMessages.text = "404 not found!"; // Sayfa bulunamadı hatası
    38.             Debug.Log("<color=red>"+ w.downloadHandler.text + "</color>");//error
    39.          
    40.         }
    41.  
    42.                else {
    43.          
    44.                 if (w.downloadHandler.text.Contains ("error")) {
    45.              
    46.                     errorMessages.text = "Geçersiz mail adresi!";
    47.                     Debug.Log("<color=red>"+ w.downloadHandler.text + "</color>");//Hatayı yazdırıyor
    48.                     Debug.Log("geçersiz mail");//Hatayı yazdırıyor
    49.  
    50.                 } else {
    51.                                Debug.Log(" Mail adresi geçerli.");
    52.                 Debug.Log("<color=green>"+ w.downloadHandler.text + "</color>");//user exist
    53.                 Debug.Log(w.downloadHandler.text);
    54.                 errorMessages.text = ""; //Error kutusunu boşaltıyor.
    55.                 progressCircle.SetActive(true); // Loading sprite aktif.
    56.  
    57.  
    58.             }
    59.          
    60.         }
    61.  
    62.         loginButton.interactable = true;
    63.         progressCircle.SetActive (false); // Loading sprite pasif işlem tamamlandı.
    64.  
    65.  
    66.         w.Dispose ();
    67.     }
    68. }
     
    Last edited: May 12, 2020
  2. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Your error handling is horrible. Why are you assuming 404 in case of any error?
    You should be checking the w.error property, that might say what happened. Also worth checking logcat.
     
  4. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Hello Aurimas,

    Thank you for your advice. I changed my error handler to "w.error" as you said.
    And it returns "Unknown Error"

    Actually I don't understand that why my project working on Unity but not in Android? :/

    How can i check LOGCAT? Where is it places in Android devices?

    Thanx...
     
  5. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Ok I have found sometihing:

    Members have said "must be turn on Internet Access" in build settings: https://forum.unity.com/threads/resolved-unknown-error-www-android-php.546529/
    (I have tried but its not working still)

    And here is the logchat usage: https://answers.unity.com/questions...-logcat.html?childToView=571021#answer-571021
    (I didnot try this still but I think my problem is about android accesses...)

    And in this topic, members have said this problem is about HTTP. I mean we have to use HTTPS :https://forum.unity.com/threads/android-9-cleartext-http-traffic-to-my_ip-not-permitted.594298/
    (But some members dont agree with this. Even so, I will buy an SSL today for Using HTTPS on my domain...)

    I am confused!... :)

    Thanx.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Is your request HTTPS? If yes, look at your Android version. That link about cleartext traffic is what you need if you use Unity older than 2019.2 and Android version is 9 or higher.
    BTW, there is a logcat package for Unity, but I don't remember the minimum version. But if you have Android Studio installed, you can use logcat from it.
     
  7. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Hello Aurimas,

    No, my URL begins with http://www.... in Unity. But i have bought an SSL certification for my domain. It will be activated a few hours later. When its activated, i'll change my URL https://www... in Unity.

    I will youknow about the result.
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    If your Android device has Android 9 or newer and Unity is older than 2019.2, then insecure HTTP request won't work, unless you add cleartext enablement in the manifest or switch to HTTPS. However, if you do have a device with older version of Android, requests should work on it.
     
  9. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    [SOLUTION IS HERE]

    Hello forum.unity members,

    The first solution is you have to use "HTTPS://www....." urls for your application. Now my application is working perfectly on android gradle build. This is the first solution. Hypertext transfer protocol secure (HTTPS) is the secure version of HTTP. And you can buy it from your domain reseller. I have bought SSL sertification for my HTTP(S) for 4.99$.
    Source:https://forum.unity.com/threads/android-9-cleartext-http-traffic-to-my_ip-not-permitted.594298/

    The second solution is, switch your build type from Gradle to Internal. And you can use this with "http://www..." urls... I did not try this method but many forum member have said that it has worked.
    Source:https://forum.unity.com/threads/resolved-unknown-error-www-android-php.546529/

    I think the first solution is better. Because if you switch your build type to "Internal" you can get some problem while uploading your application to PlayStore.

    And the special thanks to @Aurimas-Cernius :)

    Best Regards...