string from WWW, reading problem

Hi,

I have the code below witch ask in a Mysql databae if a player exist.
The user.php returns a string “yes” or “no” depending if player exist or no;
The problem is that the code in the line 18, prints normally the word (yes or no), but the next two if statements can not make the comparison and and my code cannot pass through those statements.

Simply the code from line 21 to 31 is like it never existed.

Any idea ?

Private string checkName= "user";
    Private string exist;
    public string GetNameUrl = "http://www.url.com/user.php";
   
    IEnumerator check4NameInServer(string checkName)
    {
       
        WWWForm form = new WWWForm();
        form.AddField("username",checkName);
       
        WWW www = new WWW(GetNameUrl,form);
        yield return www;

        if(www.text == "") {
            updateMessageBox ("You Are Not Connected. \nError:" + www.error);
        }else {   
            exist = www.text;
            print("-> "+exist);
        }

        if(exist.Equals("yes")){
            print ("1->pass from here");
        }else if(exist.Equals("no")){
            print ("2->pass from here");
        }
        
        if(exist == "yes")){
            print ("1->pass from here");
        }else if(exist == "no"){
            print ("2->pass from here");
        }
    }

Thank you in advance.

ki_ha1984

Are you sure there is no break line or a space after the response? It would seem the same in the output, but the comparison won’t work. Can you say what the result for print(“'” + exist + “'”); is?

the result of print(“'” + exist + “'”); is → 'yes’ UnityEngine.MonoBehaviour:print(Object)

Could be a problem with the character encoding ?

ki_ha1984

For some very crazy reason, when i copy the <yes> text from the console output (the output of the line 18) and paste it on the csharp equation of the code, line 21, the it worked for me. now i am thinking that is a problem with the encoding.
But i still have the problem because i have many texts that i have to control with if statements.

Any idea with thid problem ?

Why Private in place of private?

sorry my mistake in this text, in my code is private.
Is that a problem ?

No, no… it’s ok then! I just wanted to be sure I wasn’t missing something… :slight_smile:

So the output is “2->pass from here”, or do you see no output at all?

Seems so. I’d make sure to use the same encoding (ASCII or UTF8) on both ends.
You could also try to get the bytes array from the stream (WWW.bytes) and convert that to an encoded string to see if that helps.

  1. There no output after the output of the line 18.
  2. how to check the encoding of both ?
  3. You mean to get it as byte and then to convert to string.

Of course there’s not going to be any output in case the conditions are not met, that’s what the if statements are there for. :wink:

This seems a bit strange actually. Try something like this:

string encodedUtf = System.Text.UTF8Encoding.Default.GetString(www.bytes);

and then use that string for your statements:

string encodedUtf = System.Text.UTF8Encoding.Default.GetString(www.bytes);

if(encodedUtf.Equals("yes")){
    print ("1->pass from here");
}else if(encodedUtf.Equals("no")){
    print ("2->pass from here");
}

See what happens then.

If that doesn’t help you’ll possibly need to provide access to the API so we can see what really is returned.

i will try that soon, but now i notice that and other if statements of string, does not work, which are simple string not from WWW. now i will reinstall monodevelop and unity.

Try to simply close Unity and restart it.

I have made many restarts of unity and the pc.
But no solution. Now i will reinstall it.

I have made the changes of my code like Glockenbeat says

string encodedUtf = System.Text.UTF8Encoding.Default.GetString(www.bytes);
        print ("x-> "+encodedUtf);
        if(encodedUtf.Equals("yes")){
            print ("1->pass from here");
        }else if(encodedUtf.Equals("no")){
            print ("2->pass from here");
        }

End the output of the line 2 is strange. The output →
x-> ο»Ώyes
UnityEngine.MonoBehaviour:print(Object)

Any idea what is the letter before the yes word ?