androidunity-game-enginehttpclientunitywebrequest

UnityWebRequest does nothing on IL2CPP Build


I've been trying to simply call an api on an android build supporting 64 bit (IL2CPP build) and the UnityWebRequest class didnt seem to work. It's being called via a simple ui button click. It hits the webRequest.SendWebRequest(); and nothing happens. Ive tried the following samples. One, directly from the Unity docs for UnityWebRequest and others using standard HttpClient.

UnityWebRequest:

IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            webRequest.SetRequestHeader("Authorization", "Bearer " + API_KEY);
            yield return webRequest.SendWebRequest();
            if (webRequest.isNetworkError)
            {
                debugText.text = ": Error: " + webRequest.error;
                coroutineAllowed = false;
            }
            else
            {
                debugText.text = ":\nReceived: " + webRequest.downloadHandler.text;

                dynamic jsonObj = JsonConvert.DeserializeObject(webRequest.downloadHandler.text);
                foreach (var obj in jsonObj["businesses"])
                {
                    businessResults.Add(new Business()
                    {
                        name = (string)obj["name"],
                        image_url = (string)obj["image_url"],
                        review_count = (string)obj["review_count"],
                        rating = (string)obj["rating"],
                        Coordinates = new Coordinates()
                        {
                            Latitude = (float)obj["coordinates"]["latitude"],
                            Longitude = (float)obj["coordinates"]["longitude"]
                        },
                        price = (string)obj["price"]
                    });
                }
                debugText.text = businessResults.Count.ToString();
                //coroutineAllowed = true;
            }
            debugText.text = "getRequest 4";
        }
    }

This unfortunately did nothing at the yield return webRequest.SendWebRequest();

The next sample I tried was using HttpClient():

IEnumerator HttpClientCall(string uri) //possibly wrap in IEnumerator
    {
        debugText.text += "http coroutine started" +Environment.NewLine;
        using (var httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", API_KEY);

            var response = httpClient.GetAsync(uri);
            if (response.Result.StatusCode != HttpStatusCode.OK)
            {
                debugText.text += "FAILED HTTP GET";
            }
            yield return response.Result.Content.ReadAsStringAsync();
            dynamic jsonObj = JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result);
            foreach (var obj in jsonObj["businesses"])
            {
                businessResults.Add(new Business()
                {
                    name = (string)obj["name"],
                    image_url = (string)obj["image_url"],
                    review_count = (string)obj["review_count"],
                    rating = (string)obj["rating"],
                    Coordinates = new Coordinates()
                    {
                        Latitude = (float)obj["coordinates"]["latitude"],
                        Longitude = (float)obj["coordinates"]["longitude"]
                    },
                    price = (string)obj["price"]
                });
                debugText.text += Environment.NewLine + ((string)obj["name"]);
            }
        }
    }

Once again, nothing when it hits yield return response.Result.Content.ReadAsStringAsync();

These all work on PC, and they both return results that i'm expecting.

The next thing i heard was about setting the android manifest application tag with android:usesCleartextTraffic="true"

This unfortunately, also did nothing for me lol. I know it has to be the 64 support, because this works on a standard build. The moment i go to build with 64 support, it doesnt work.

Any help on why it's not returning appropriately would be very helpful.

side note, i know the code is pretty ugly, but after i can figure out why the build doesnt work on the device a heavy refactoring is going to be in play. Thanks in advance!


Solution

  • So after a lot of trouble shooting ive found out why this was not working. The main issue seems to be stemming from my use of the standard Newtonsoft Json package when Unity, apparently, has their own internal JsonUtility class. After changing this:

    dynamic jsonObj = JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result);
    

    To This:

    var js = JsonUtility.FromJson<T>(response.Result.Content.ReadAsStringAsync().Result);
    

    my results are finally showing in the the apk build correctly.

    Also, to note that to map correctly, the JsonUtility.FromJson must be typed to a class that exactly mirrors the incoming json object explicitly.

    The page article that finally helped me with this issue is here.

    P.S. Thank you to @RetiredNinja for trying to help instead of just downvoting and saying nothing of value. You're amazing!