0

I send a HTTP request and receive a JSON. I have the JSON format outlined in a class structure. Linq queries this json and submits the powers (variables) for the next scene.

This works for my Unity editor when I run on PC. However, when I build and run on my Android, it doesn't return any values from the query, even though the HTTP request is successful.

private void SendJson(string currPlayer)
    {
        string bodyJsonString = "{\"player_id\":\"" + currPlayer + "\"}";
        string currURI = djangoApi + userOwnedMarbles;
        StartCoroutine(PostRequestCoroutine(currURI, bodyJsonString));
    }

    private IEnumerator PostRequestCoroutine(string url, string json)
    {
        var jsonBinary = System.Text.Encoding.UTF8.GetBytes(json);

        DownloadHandlerBuffer downloadHandlerBuffer = new DownloadHandlerBuffer();

        UploadHandlerRaw uploadHandlerRaw = new UploadHandlerRaw(jsonBinary);
        uploadHandlerRaw.contentType = "application/json";

        UnityWebRequest www =
            new UnityWebRequest(url, "POST", downloadHandlerBuffer, uploadHandlerRaw);

        yield return www.SendWebRequest();

        if (www.isNetworkError)
            Debug.LogError(string.Format("{0}: {1}", www.url, www.error));
        else
        {
            string test = string.Format("{0}", www.downloadHandler.text);
            JObject myJson = JObject.Parse(test);
            Debug.Log("welcome" + test);

            var welcome2 = Welcome.FromJson(www.downloadHandler.text);

            CurrPower.numberColorBombs = welcome2.Marbles.Count(n => n.Nickname.ToLower() == "Corkscrew Swirl".ToLower());
            CurrPower.myPowerups[0] = CurrPower.numberColorBombs;

            CurrPower.numberVerticalBombs = welcome2.Marbles.Count(n => n.Nickname.ToLower() == "Clearie".ToLower());
            CurrPower.myPowerups[1] = CurrPower.numberVerticalBombs;

            CurrPower.numberHorizontalBombs = welcome2.Marbles.Count(n => n.Nickname.ToLower() == "Cloud".ToLower());
            CurrPower.myPowerups[2] = CurrPower.numberHorizontalBombs;

            CurrPower.numberAdjacentBombs = welcome2.Marbles.Count(n => n.Nickname.ToLower() == "Onionskin".ToLower());
            CurrPower.myPowerups[3] = CurrPower.numberAdjacentBombs;

            CurrPower.numberSingleBombs = welcome2.Marbles.Count(n => n.Nickname.ToLower() == "Bullseye".ToLower());
            CurrPower.myPowerups[4] = CurrPower.numberSingleBombs;
        }            
    }

Welcome2 contains the partial class that has all the elements of the json object. Does Android not handle LINQ well?

Here the the AndroidLauncherManifest.xml file that I've edited

<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools" android:installLocation="preferExternal">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:usesCleartextTraffic="true" tools:ignore="GoogleAppIndexingWarning" android:label="@string/app_name" android:icon="@mipmap/app_icon" />
  <uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
</manifest>

KaneHarry729
  • 95
  • 1
  • 2
  • 12

1 Answers1

1

This is a well known issue for android devices. A Google update last year made android devices with version 9.0(pie) or higher accept only HTTPS requests from all apps by default.

You can solve this by modifying the XML manifest file. This is what you need to add to it:

android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />

NOTE some of this tags may alread exist and you just need to modify the properties. If you get lost you can see this link for a bit of more explanation

Hope it helped :)

mihoci10
  • 429
  • 3
  • 16
  • I see LauncherManifest.xml and UnityManifest.xml file, which one should I add to? Note, I copied them from the Unity editor folder into the project's Assets/Plugins/Android folder – KaneHarry729 Sep 06 '19 at 20:49
  • You need to edit AndroidManifest.xml it should be located in Assets/Plugins/Android/ If it's not there you have to copy it over to this exact location from {ProjectFolder}/Temp/StagingArea/AndroidManifest.xml Once you have it in your Assets/Plugins/Android folder just edit it like written in the answer – mihoci10 Sep 08 '19 at 06:03
  • I found the file, it's actually called AndoridLauncherManifest-main.xml. (Don't think it matters with the `-main' part). I copied it to Assets/Plugins/Android and made the changes, but I still don't get any connection. Upon further examination, it just gets stuck at the yield return www.SendWebRequest(); part. I've edited my post to show what my manifest file looks like. – KaneHarry729 Sep 09 '19 at 18:01
  • And did you remove the comment on the second line to prevent overwriting? – mihoci10 Sep 10 '19 at 04:45