- Home /
Having issues with WWW and waiting for response.
Hey All,
I am struggling with doing a simple web request and waiting for the response. My game just locks up and I have to kill it via the task manager. I am very new to Unity but am very familiar with .NET so i think thats helping the frustration which is blinding me to what im assuming is a very simple solution.
Here is what i have so far (after several failed attempts so please excuse my messy code):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WebFunctions : MonoBehaviour
{
private bool hasResponse;
private WWW responseObject;
private string currentURI;
private WWWForm currentForm;
IEnumerator SendGetRequest()
{
hasResponse = false;
WWW w = new WWW(currentURI);
yield return w;
responseObject = w;
hasResponse = true;
}
IEnumerator SendPostRequest()
{
hasResponse = false;
WWW w = new WWW(currentURI, currentForm);
yield return w;
responseObject = w;
hasResponse = true;
}
public WWW Post(string URI, WWWForm form)
{
WaitForSeconds n;
currentURI = URI;
currentForm = form;
StartCoroutine(SendPostRequest());
while (!hasResponse)
n = new WaitForSeconds(0.1f);
return responseObject;
}
public WWW Get(string URI)
{
WaitForSeconds n;
currentURI = URI;
StartCoroutine(SendGetRequest());
while (!hasResponse)
n = new WaitForSeconds(0.1f);
return responseObject;
}
}
Any and all help would be appreciated.
EDIT: With the help of Chris i managed to get it to work the way i wanted, here is the solution:
using UnityEngine;
using System.Collections.Generic;
public class WebFunctions
{
public static WWW Get(string url)
{
WWW www = new WWW (url);
WaitForSeconds w;
while (!www.isDone)
w = new WaitForSeconds(0.1f);
return www;
}
public static WWW Post(string url, Dictionary<string,string> post)
{
WWWForm form = new WWWForm();
foreach(var pair in post)
form.AddField(pair.Key, pair.Value);
WWW www = new WWW(url, form);
WaitForSeconds w;
while (!www.isDone)
w = new WaitForSeconds(0.1f);
return www;
}
}
And you simply call it like this from your code & it will wait for a response before executing your next line of code:
var response = WebFunctions.Get("http://www.google.com");
// right here you can access response.text or any other property
SomeRandomTextComponent.text = response.text;
Your WebFunctions works like a charm. $$anonymous$$y www class also had super slow response. I send post request to the server and have to wait for the response which contains data that I have to use afterward. But, I don't know why yield return is slow in my case. Thank you, man!
Answer by Chris333 · Jan 25, 2015 at 11:36 AM
Hi,
did you already read the docs about it? http://docs.unity3d.com/ScriptReference/WWW.html
There it says:
You can inspect the isDone property to see if the download has completed or yield the download object to automatically wait until it is (without blocking the rest of the game).
You can give your elements, ui's or whatever you using, a default content and after the yield returns the www response set that New content directly in the coroutine.
Hey, thanks for the reply. I see what you are saying however I am trying to make it a synchronous call so execution will wait for the response. I continued looking into it and I think I might have a solution but I have yet to try. If it does end up working out ill post my solution but if you have any more ideas I would be more than happy to hear them. Thanks.
Hey Chris,
Thanks again for the reply, WWW.isDone was what i was looking for. You Rock. I have edited my OP to include my solution.
Thanks again for the help.
Answer by synchro_suban · Feb 22, 2018 at 05:17 PM
This is what i did in my game
IEnumerator ressponceCoroutine()
{
WWW w = new WWW("Your Api URl");
yield return new WaitUntil(() => w.bytesDownloaded > 0);
TextComponent.text = w.text;
}
// Call in Start Method
void Start ()
{
StartCoroutine(ressponceCoroutine());
}
@synchro_suban Thanks for your help.
your solution helped me a lot. Thanks.
Your answer