Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

string from WWW, reading problem

Discussion in 'Scripting' started by ki_ha1984, Dec 12, 2014.

  1. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    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 ?

    Code (CSharp):
    1. Private string checkName= "user";
    2.     Private string exist;
    3.     public string GetNameUrl = "http://www.url.com/user.php";
    4.    
    5.     IEnumerator check4NameInServer(string checkName)
    6.     {
    7.        
    8.         WWWForm form = new WWWForm();
    9.         form.AddField("username",checkName);
    10.        
    11.         WWW www = new WWW(GetNameUrl,form);
    12.         yield return www;
    13.  
    14.         if(www.text == "") {
    15.             updateMessageBox ("You Are Not Connected. \nError:" + www.error);
    16.         }else {  
    17.             exist = www.text;
    18.             print("-> "+exist);
    19.         }
    20.  
    21.         if(exist.Equals("yes")){
    22.             print ("1->pass from here");
    23.         }else if(exist.Equals("no")){
    24.             print ("2->pass from here");
    25.         }
    26.        
    27.         if(exist == "yes")){
    28.             print ("1->pass from here");
    29.         }else if(exist == "no"){
    30.             print ("2->pass from here");
    31.         }
    32.     }
    Thank you in advance.

    ki_ha1984
     
  2. Megolas

    Megolas

    Joined:
    Apr 6, 2013
    Posts:
    25
    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?
     
  3. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    the result of print("'" + exist + "'"); is -> 'yes' UnityEngine.MonoBehaviour:print(Object)
     
  4. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Could be a problem with the character encoding ?

    ki_ha1984
     
  5. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    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 ?
     
  6. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Why Private in place of private?
     
  7. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    sorry my mistake in this text, in my code is private.
    Is that a problem ?
     
  8. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    No, no... it's ok then! I just wanted to be sure I wasn't missing something... :)
     
  9. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    817
    So the output is "2->pass from here", or do you see no output at all?
     
  10. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    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.
     
  11. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    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.
     
  12. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    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. ;-)

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

    Code (CSharp):
    1. string encodedUtf = System.Text.UTF8Encoding.Default.GetString(www.bytes);
    and then use that string for your statements:

    Code (CSharp):
    1.  
    2. string encodedUtf = System.Text.UTF8Encoding.Default.GetString(www.bytes);
    3.  
    4. if(encodedUtf.Equals("yes")){
    5.     print ("1->pass from here");
    6. }else if(encodedUtf.Equals("no")){
    7.     print ("2->pass from here");
    8. }
    9.  
    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.
     
  13. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    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.
     
  14. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Try to simply close Unity and restart it.
     
  15. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    I have made many restarts of unity and the pc.
    But no solution. Now i will reinstall it.
     
  16. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    I have made the changes of my code like Glockenbeat says

    Code (CSharp):
    1. string encodedUtf = System.Text.UTF8Encoding.Default.GetString(www.bytes);
    2.         print ("x-> "+encodedUtf);
    3.         if(encodedUtf.Equals("yes")){
    4.             print ("1->pass from here");
    5.         }else if(encodedUtf.Equals("no")){
    6.             print ("2->pass from here");
    7.         }
    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 ?