我正在做一个apk,当我在手机上运行它时会出现以下错误
“未知错误”
但最奇怪的是,如果我从统一运行它时它可以正常工作,我使用以下代码向我展示了我在手机上执行它时出现的错误,因为统一运行完美
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");
}
}
}
检查我的 apk 是否已连接到 Internet,如果它连接到 Internet,则使用以下代码显示文本
private void Update()
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
advertencia.SetActive(true);
Debug.Log("Error. Check internet connection!");
}
}
代码更完整一点:
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));
}