6

I'm using Unity 3D's WWW to make http requests: http://docs.unity3d.com/ScriptReference/WWW.html

It seems that no matter what kind of data I'm trying to access, it just returns: ���� every time. I've tried json files, I've tried php that just generates a string. I can't seem to access the values on the server.

C#:

public string url = "http://www.onelittledesigner.com/data.php";

IEnumerator Start() {
    WWW www = new WWW(url);
    yield return www;

    if (!string.IsNullOrEmpty(www.error)) {
        Debug.Log(www.error);
    } else {
        Debug.Log(www.text);
    }

}

PHP:

<?php
  echo "textiness";
?>

Note: I've used www.texture successfully to pull images off of the server. However, www.text doesn't seem to be working.

mmm
  • 2,272
  • 3
  • 25
  • 41
  • I don't use C#, but shouldn't you be converting `www.error` and `www.text` to strings first? To me it sounds like the `Debug.Log` function is accepting your variable, but your variable isn't in the right format. – AStopher Jul 19 '14 at 15:15
  • They're strings already by default, I checked. – mmm Jul 19 '14 at 15:17
  • try: Debug.Log("text: " + www.text); or Debug.Log(www.text.ToString()); to force a string conversion – CodeSmile Jul 19 '14 at 15:18
  • @LearnCocos2D This is the result I get -> "text: ����" – mmm Jul 19 '14 at 15:19
  • @hellaFont Does string().c_str() exist in C#? If so, try using this. It's a long shot, but it might just produce the result you're expecting. – AStopher Jul 19 '14 at 15:26
  • @hellaFont Just had an idea. Please go to the variable's definition, and paste it here/check that it actually is a string. – AStopher Jul 19 '14 at 15:27
  • 1
    @zyboxinternational There's no string().c_str() :/ but that was a really good idea. – mmm Jul 19 '14 at 15:28
  • Aha, I think I see a few possible solutions: http://docs.unity3d.com/ScriptReference/WWW-text.html – mmm Jul 19 '14 at 15:30
  • I tried `System.Text.Encoding.Default.GetString(www.bytes)`, with all the different encodings (UTF8, ASCII, UTF7, UTF32, UNICODE). Nothing... I didn't get the same results, but they weren't the correct results. – mmm Jul 19 '14 at 15:35
  • @hellaFont Might this be of use to you? http://docs.unity3d.com/ScriptReference/WWW-bytes.html – AStopher Jul 19 '14 at 15:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57605/discussion-between-hellafont-and-zyboxinternational). – mmm Jul 19 '14 at 15:54
  • [No repro](http://i.stack.imgur.com/bSidV.png). Works just fine. So something else must be going wrong if it still doesn't work for you. – Bart Jul 19 '14 at 20:55
  • @Bart could it be something to do with the network I'm on? – mmm Jul 20 '14 at 09:14
  • If you dump that link in your browser, what do you get? – Bart Jul 20 '14 at 09:23
  • I get textiness, as expected. Perhaps it's the version of Unity I'm on? 4.3.1 Pro. – mmm Jul 20 '14 at 10:43
  • Hmm, I'm on 4.5.2 here. Perhaps try the upgrade? There have been tons of fixes to WWW between those versions. – Bart Jul 20 '14 at 11:56
  • @hellaFont Did you try the code from the question in a new project? Are you testing the same code or only something similar in your bigger project? – Stefan Hoffmann Jul 20 '14 at 12:54
  • @Leosori that's a great idea, I'll give it a try in an empty project and post the results later. – mmm Jul 20 '14 at 21:34
  • 1
    Try posting the result of `string s=""; foreach(var b in www.bytes) s+=(b+" "); Debug.Log(s);`. – Krzysztof Bociurko Jul 22 '14 at 09:48
  • `116 101 120 116 105 110 101 115 115` === textiness, it totally worked! thanks @ChanibaL! put it into an answer and I'll accept it :) – mmm Jul 22 '14 at 17:00

2 Answers2

2

Copying answer from comment, with some additional tests. My results are in the comments. Note that using Default, ASCII or UTF8 does work on my machine - it also should on yours.

    // returned from www.bytes, copied here for readability
    byte[] bytes=new byte[]{116, 101, 120, 116, 105, 110, 101, 115, 115};

    string customDecoded=""; 
    foreach(var b in bytes)
        customDecoded+=(char)b; 

    Debug.Log(customDecoded); // textiness
    Debug.Log(System.Text.Encoding.Default); // System.Text.ASCIIEncoding
    Debug.Log(System.Text.Encoding.Default.GetString(bytes));  // textiness
    Debug.Log(System.Text.Encoding.ASCII.GetString(bytes));  // textiness
    Debug.Log(System.Text.Encoding.Unicode.GetString(bytes)); // 整瑸湩獥
    Debug.Log(System.Text.Encoding.UTF7.GetString(bytes)); // textiness
    Debug.Log(System.Text.Encoding.UTF8.GetString(bytes)); // textiness
    Debug.Log(System.Text.Encoding.UTF32.GetString(bytes)); // 整湩
    Debug.Log(System.Text.Encoding.BigEndianUnicode.GetString(bytes)); // 瑥硴楮敳

Please check if System.Text.Encoding.Default is ASCIIEncoding, maybe something changed the default value?

Krzysztof Bociurko
  • 4,575
  • 2
  • 26
  • 44
-2

My answer assumes this script is attached to a MonoBehaviour. The reason it's not working is because you tried to turn Start method into a co-routine, but did it only half way.

Here is what you need to do

private void Start()
{
   StartCoroutine(StartCR());
}

IEnumerator StartCR() 
{
   WWW www = new WWW(url);
   yield return www;

   if (!string.IsNullOrEmpty(www.error)) {
    Debug.Log(www.error);
   } else {
    Debug.Log(www.text);
   }
}

Unity will call the Start method which in turn will correctly call your co-routined WWW code. This will wait for your web-response to complete instead of just returning nothing.

Alex
  • 1,110
  • 8
  • 15
  • Answer invalid - the corountine is started by Unity if the `Start` method is `IEnumerator Start()`. See second example on http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html – Krzysztof Bociurko Jul 25 '14 at 10:46
  • My bad din't realize you were using js. Still the answer is valid in c# :) – Alex Jul 25 '14 at 10:53
  • HellaFont's code is clearly marked as C# and `IEnumerator Start()` is a C# construct (unityscript guesses the return type). There is no js/unityscript here. – Krzysztof Bociurko Jul 25 '14 at 10:57
  • Never saw it work like that. But fairly certain it will work this way. – Alex Jul 25 '14 at 11:09
  • To clarify: yes, your code is valid but it is not fixing any of OP's problems - therefore it is not a correct answer. – Krzysztof Bociurko Jul 25 '14 at 11:26