Search Unity

WWW is return empty (www.text)

Discussion in 'Scripting' started by QuantumRevenger, Feb 9, 2016.

  1. QuantumRevenger

    QuantumRevenger

    Joined:
    Mar 22, 2014
    Posts:
    25
    Issue is that nothing is being returnfrom:
    Code (CSharp):
    1. return www.text;
    Code (CSharp):
    1.    // Location Information
    2. locationInformation = GET("https://www.domain.co.uk/api/locationInformation.php?secret=" + apiSecret + "&latitude=" + GPS.latitude.ToString() + "&longitude=" + GPS.longitude.ToString());
    3.  
    Code (CSharp):
    1.  public string GET(string url)
    2.     {
    3.  
    4.         WWW www = new WWW(url);
    5.         StartCoroutine(WaitForRequest(www));
    6.         return www.text;
    7.     }
    Output from browser:
    The PHP Script (output section)
    Code (CSharp):
    1. // OUTPUT IS: POSTCODE,CITY/TOWN,COUNTY (NEAREST TO GPS POSITION)
    2.  
    3. echo $jsonData->result[0]->postcode;
    4. echo ",";
    5. echo $jsonData->result[0]->parish;
    6. echo ",";
    7. echo $jsonData->result[0]->primary_care_trust;
     
  2. QuantumRevenger

    QuantumRevenger

    Joined:
    Mar 22, 2014
    Posts:
    25
    It won't let me update/edit my post, this is the WaitForRequest(www) code:

    Code (CSharp):
    1.  private IEnumerator WaitForRequest(WWW www)
    2.     {
    3.         yield return www;
    4.     }
     
  3. QuantumRevenger

    QuantumRevenger

    Joined:
    Mar 22, 2014
    Posts:
    25
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Your problem is that you don't wait for the download to finish before you return www.text, which at this point would be null or empty. Starting a coroutine does not make code outside of that routine wait for execution like in your GET method.
    You will need to use another way of returning the text from the download at a later time rather than the immediate return you're using. You could supply a delegate method to run with www.text in the coroutine after the download has finished.
    Something along these lines:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestingWWW : MonoBehaviour
    6. {
    7.     public string url = "https://www.simplygrub.co.uk/api/locationInformation.php?secret=hello&latitude=xxxxx&longitude=xxxxxx";
    8.  
    9.     private delegate void TextDelegate(string text);
    10.  
    11.     private void Start ()
    12.     {
    13.         StartCoroutine(DownloadTest(url, PrintText));
    14.     }
    15.  
    16.     private IEnumerator DownloadTest(string url, TextDelegate output)
    17.     {
    18.         WWW www = new WWW(url);
    19.         yield return www;
    20.         output(www.text);
    21.     }
    22.  
    23.     private void PrintText(string text)
    24.     {
    25.         Debug.Log(text);
    26.     }
    27. }
    28.  
     
    Last edited: Feb 9, 2016
  5. QuantumRevenger

    QuantumRevenger

    Joined:
    Mar 22, 2014
    Posts:
    25
    Thank you, the problem is now resolved :)
     
  6. dradb

    dradb

    Joined:
    Jan 10, 2015
    Posts:
    86
    I'm having the same problem, except that I'm using a form:

    WWWForm form = new WWWForm();
    form.AddField("coypwd", CoyPasswd);
    WWW w = new WWW("http://www.xxxx.com/getName.php", form);

    The delegate method works fine on a PC in the editor, but fails when built to WebGL. Nothing is returned with WebGL.

    Any help is appreciated.
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    What error does WWW give to you?
    Note, that on WebGL you cannot do requests to other hosts than your game, unless you setup CORS accordingly.