Www.text returns weird value in Unity3D (C #)

I use Unity 3D WWW to create http requests: http://docs.unity3d.com/ScriptReference/WWW.html

It seems that no matter what data I try to get, it just returns: every time. I tried json files, I tried php, which just generates a string. I cannot access the values ​​on the server.

WITH#:

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 used www.texture to delete images from the server. However, www.text does not seem to work.

+6
source share
2 answers

Copy response from comment with some additional tests. My results are given in the comments. Please note that using default, ASCII or UTF8 really works on my machine - it should also be 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 ASCIIEncoding, maybe something changed the default value?

+2
source

My answer suggests that this script is attached to MonoBehaviour. The reason it doesn't work is because you tried to turn the Start method into a collaborative procedure, but only half done it.

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-programmed WWW code. This will wait for your web response to complete, instead of just returning nothing.

-2
source

Source: https://habr.com/ru/post/972504/


All Articles