0
votes

Building a Unity front-end app that communicates with a PHP/MySQL back-end. Right now I am working on the register and login portions of it. In the Unity editor, all works perfectly, on the browser front end registration is working fine as well. But as soon as I take the working build in Unity and build it to test on my Google Pixel phone, registration and login both fail on my phone. The error message I am getting on Login is "Error parsing response from server, please try again!" which is in my UILogin.cs. Yet there is nothing else attached. I tried following the Unity documentation for debugging, adb, and DDMS to get access to find out what is happening but I have failed on all those ventures. Is there something particularly different about Android and WWW objects or web communications? Here is a portion of my UILogin.cs and also will include the Login.php.

UILogin.cs

IEnumerator AttemptLogin(bool quickLogin)
    {
        CreateLoadingScreen();
        DeactivateForm();
        WWWForm form = new WWWForm();
        form.AddField("email", Email.text);
        form.AddField("password", Password.text);

        WWW www = new WWW(URL.GetLoginURL, form);
        yield return www;

        DestroyLoadingScreen();
        ActivateForm();
        ParseResult(www.text, quickLogin);
    }

    void ParseResult(string result, bool quickLogin)
    {
        byte resultCode;
        if (!byte.TryParse(result, out resultCode))
            Result.text = "Error parsing response from server, please try again!";
        else if (resultCode == 0)
        {
            if (RememberToggle.isOn && !quickLogin) // Remember me
            {
                PlayerPrefs.SetInt("remember", 1);
                PlayerPrefs.SetString("email", Email.text);
                PlayerPrefs.SetString("password", Password.text);
            }
            else if (!quickLogin)
            {
                TemporaryAccount.Email = Email.text;
                TemporaryAccount.Password = Password.text;
            }

            SceneManager.LoadScene(3);
        }
        else // Failure
        {
            if (quickLogin)
                ShowForm();

            Result.text = WebError.GetLoginError(resultCode);
        }
    }

Login.php

<?php
    require "conn.php";

    $stmt = $pdo->prepare("SELECT * FROM account WHERE email=:email");
    $stmt->bindParam(":email", $_POST['email']);
    $stmt->execute();
    $count = $stmt->rowCount(); // gets count of records found

    if($count > 0) {
        $result = $stmt->fetch(); // gets resultset
        if(!password_verify($_POST['password'], $result[4]))
            echo "2";
        else
            echo "0";
    }
    else {
        echo "1";
    }
?>

Any ideas?

Update #1 I printed www.text and www.error after the yield return www; Text is null and error says: Unknown Error.

Update #2 Interestingly enough, I get Unknown Error from this as well:

Clickme.cs

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Clickme : MonoBehaviour
{
    public Text Result;

    public void OnClick()
    {
        StartCoroutine(RunScript());
    }

    IEnumerator RunScript()
    {
        WWW www = new WWW("http://www.familypolaris.com/helloWorld.php");
        yield return www;

        if (www.text == "")
            Result.text = "Result was empty, error: " + www.error;
    }
}

HelloWorld.php

<?php
    echo "Hello World!";
?>
1
The first thing you should do is print the received data www.text and see if it actually received the expected data. I doubt that it did on Android. The second thing to do is to print the error www.error. You must check if there is an error in your request before processing or using the received data. Update your qustion with the result.Programmer
Okay I did it and updated my question. The results of printing www.text and www.error after the yield were thus: www.text was empty. www.error said: "Unknown Error"Jeremy Voss
Well that's your issue. The WWW request is failing. What is the value of URL.GetLoginURL when you log it on Android?Programmer
Here is something funny. So I tested it out by super simplifying it. I get the Unknown error for this code as well: I'll update my question with it, give me a sec. Okay added Update #2. I don't think we could simplify the code much more than that! Hahahaha. Yet the "Unknown Error" remains, even in this simple code.Jeremy Voss
Also URL is a class I created, not one of the official classes. URL.GetLoginURL is an accessor that returns this string: familypolaris.com/login.php (stackOverflow kills the h t t p : / / w w w part of it, but it is there.)Jeremy Voss

1 Answers

0
votes

I figured it out, so I will post the answer here. After somebody commented that they tried my code and it worked just fine, I decided to try another device here. That device also failed to register and login. This told me something was wrong with my build settings. My first idea was to switch build type from Gradle to Internal, and that solved the problem for both my devices immediately.