Linked Questions

Popular Questions

Unity - Get HTTP request (UnityWebRequest) doesn't work

Asked by At

For a mobile app I want to make a call to a webpage, for which I'm using UnityWebRequest (which supports Android according to the Unity docs (https://docs.unity3d.com/Manual/UnityWebRequest.html)). When testing in the editor it works flawlessly, but as soon as I test it on my Android device, nothing seem to happen.

Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class API : MonoBehaviour
{
    public Text displayResultText;

    public void SendRequestClick(){
        Coroutine routine = StartCoroutine(TestNewRoutineGivesException());
    }

    IEnumerator TestNewRoutineGivesException()
    {
        UnityWebRequest www = UnityWebRequest.Get("www.google.com");
        www.downloadHandler = new DownloadHandlerBuffer();
        yield return www.SendWebRequest();
        while (!www.downloadHandler.isDone)
            yield return new WaitForEndOfFrame();

        if (www.isNetworkError)
        {
            Debug.Log(www.error);
        }
        else
        {
            string results = www.downloadHandler.text;
            DoSomethingWithTheCoroutineResult(results);
        }
        yield break;
    }

    void DoSomethingWithTheCoroutineResult(string results)
    {
        Debug.Log("Successfully got the result: " + results);
        displayResultText.text = results;
    }
}

So the Get HTTP request should print to a text field, but it doesn't do as such when testing the app on Android. Anybody that could figure out how to get it to work on Android?

Much appreciated!

Related Questions