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:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Networking;
public class Server : MonoBehaviour
{
[SerializeField] InputField Mail;
[SerializeField] Text errorMessages;
[SerializeField] GameObject progressCircle;
[SerializeField] Button loginButton;
[SerializeField] string url;
WWWForm form;
public void ServerBaslat ()
{
loginButton.interactable = false;
progressCircle.SetActive (true);
StartCoroutine(Login());
}
IEnumerator Login ()
{
form = new WWWForm ();
form.AddField ("mail", Mail.text);
UnityWebRequest w = UnityWebRequest.Post (url, form);
yield return w.SendWebRequest();
if (w.isNetworkError || w.isHttpError) {
errorMessages.text = "404 not found!"; // Sayfa bulunamadı hatası
Debug.Log("<color=red>"+ w.downloadHandler.text + "</color>");//error
}
else {
if (w.downloadHandler.text.Contains ("error")) {
errorMessages.text = "Geçersiz mail adresi!";
Debug.Log("<color=red>"+ w.downloadHandler.text + "</color>");//Hatayı yazdırıyor
Debug.Log("geçersiz mail");//Hatayı yazdırıyor
} else {
Debug.Log(" Mail adresi geçerli.");
Debug.Log("<color=green>"+ w.downloadHandler.text + "</color>");//user exist
Debug.Log(w.downloadHandler.text);
errorMessages.text = ""; //Error kutusunu boşaltıyor.
progressCircle.SetActive(true); // Loading sprite aktif.
}
}
loginButton.interactable = true;
progressCircle.SetActive (false); // Loading sprite pasif işlem tamamlandı.
w.Dispose ();
}
}
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.
And in this topic, members have said this problem is about HTTP. I mean we have to use HTTPS : Android 9 : Cleartext HTTP traffic to [MY_IP] not permitted
(But some members dont agree with this. Even so, I will buy an SSL today for Using HTTPS on my domain…)
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.
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.
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.
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: Android 9 : Cleartext HTTP traffic to [MY_IP] not permitted
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: [RESOLVED]Unknown Error (WWW, Android, PHP)
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.