Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Android UnityWebRequest Error Code 0 - Works Fine in iOS and Editor

Discussion in 'Scripting' started by adaptivemobiledevelopment, Mar 24, 2019.

  1. adaptivemobiledevelopment

    adaptivemobiledevelopment

    Joined:
    Mar 20, 2018
    Posts:
    30
    I'm having in issue with Android only. I am trying to post to a server with Json data. My code works fine on iOS and in the editor but fails on Android. When I try it on my Android device it fails with error code 0. It's funny it is only on this one POST request others seem to be function fine. CORS is turned on, and it works everywhere else but on Android. Any Ideas? The code is below.

    Code (CSharp):
    1.  
    2.  
    3.         Uri myUri = new Uri("https://www.websitename.com/api/CreateMultiplayerGame");
    4.         using (UnityWebRequest request = new UnityWebRequest(myUri))
    5.         {
    6.             DownloadHandlerBuffer dh = new DownloadHandlerBuffer();
    7.             request.downloadHandler = dh;
    8.             string authorization = authenticate("user", "password");
    9.             request.SetRequestHeader("AUTHORIZATION", authorization);
    10.             request.method = UnityWebRequest.kHttpVerbPOST;
    11.             request.timeout = 20;
    12.             request.uploadHandler = new UploadHandlerRaw(Encoding.ASCII.GetBytes(JsonUtility.ToJson(game)));
    13.             request.uploadHandler.contentType = "application/json";
    14.             yield return request.SendWebRequest();
    15.             if (!request.isNetworkError && !request.isHttpError)
    16.             {
    17.                 Debug.Log("Created Game -- Update the UI");
    18.                 Debug.Log(request.downloadHandler.text);
    19.                 var multiplayerStatus = JsonUtility.FromJson<MultiplayerStatusModel>(request.downloadHandler.text);
    20.  
    21.                 //update the multiplayer panel with the status information.
    22.                 revenueCover.SetActive(false);
    23.                 multiplayerMessagePanel.SetActive(false);
    24.                 loadingMultiplayerSpinner.SetActive(false);
    25.  
    26.                 click.multiplayerPendingExpiration = false;
    27.                 click.multiplayerGoalAmount = multiplayerStatus.GoalAmount;
    28.                 click.multiplayerGameTypeId = multiplayerStatus.GameTypeId;
    29.                 click.multiplayerGameId = multiplayerStatus.GameId;
    30.                 click.multiplayerGameNickName = gameNickNameInput.text.ToLower();
    31.                 click.multiplayerRewardTypeId = multiplayerStatus.RewardTypeId;
    32.                 click.multiplayerRewardAmount = multiplayerStatus.RewardAmount;
    33.                 MultiplayerPlayers = multiplayerStatus.PlayerTotals;
    34.  
    35.                 UpdateMultiplayerUIStats(multiplayerStatus);
    36.             }
    37.             else
    38.             {
    39.                 Debug.Log("Failed to create game!");
    40.                 Debug.Log(request.error);
    41.                 Debug.Log(request.responseCode);
    42.                 Debug.Log(request.downloadHandler.text);
    43.  
    44.                 if (request.responseCode == (long)HttpStatusCode.Forbidden)
    45.                 {
    46.                     multiplayerPanelText.text = JsonUtility.FromJson<httpErrorMessage>(request.downloadHandler.text).Message;
    47.                 }
    48.                 else
    49.                 {
    50.                     multiplayerPanelText.text = "Unknown Error, please try again later.";
    51.                 }
    52.  
    53.                 multiplayerPanelNobtn.SetActive(false);
    54.                 multiplayerPanelYesbtn.SetActive(false);
    55.                 multiplayerPanelLeavebtn.SetActive(false);
    56.                 multiplayerPanelClosebtn.SetActive(true);
    57.                 loadingMultiplayerSpinner.SetActive(false);
    58.             }
    59. }
     
  2. adaptivemobiledevelopment

    adaptivemobiledevelopment

    Joined:
    Mar 20, 2018
    Posts:
    30
    Ok so it is posting to the server and I can see it is adding the game. However the response on Android is showing an error on the return when really it should be a 200 ok response. Why would android see a 200 as an error?
     
  3. adaptivemobiledevelopment

    adaptivemobiledevelopment

    Joined:
    Mar 20, 2018
    Posts:
    30
    Another add. isnetworkerror = true but the http error = false. I have no idea how that is possible. So http is returning 200 people aneroid thinks it has a network error?
     
  4. adaptivemobiledevelopment

    adaptivemobiledevelopment

    Joined:
    Mar 20, 2018
    Posts:
    30
    Tried switching to wait on downloadhandler.isdone and it never happens on my Android phone. Works fine in editor and on iOS. Will not work on Android.
     
  5. UmerFS

    UmerFS

    Joined:
    May 29, 2018
    Posts:
    2
    I have a similar issue, I have a get request which works on the Unity Editor (2018.4.8f1) and on iOS, but fails on android.

    I've tried setting the clear text option (cleartextTrafficPermitted="true") inside the androidmanifest.xml and it did not work.

    Edit: Fixed the issue!
    There seems to be a difference in using the UnityWebRequest class specifically the GET method on android.

    I did a debug of the webRequest.downloadhandler.text and noticed that the UWR was using post despite me explicitly stating GET.

    I deleted the webRequest.uploadHandler = new UploadHandlerRaw(byteData) and it worked!

    Strange thing is I can leave the above line in compilation for iOS and the Unity Editor.
     
    Last edited: Mar 3, 2020
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Avoid that.
    Some backend simply don't support sending body data using GET request. I know for sure that NSURLSession (which we use in Unity 2019.x and later on iOS) does not allow it. In Editor in 2018.4 we ignore the body, but in latest version we do support sending body using GET. Android should work like Editor in latest versions.
     
  7. UmerFS

    UmerFS

    Joined:
    May 29, 2018
    Posts:
    2
    Hi Aurimas,

    Upgrading to the latest Unity Editor was my initial thought, however it broke other dependencies in my project linked to the AWS SDK. Link to issue.

    To give some context around the issue, I created a postman like editor within my Unity project to provide webhook functionality. I was confused with Unity converting the GET request to a POST despite the body being {} and using GET within the UWR.
    Thanks for letting me know of the changes to the native iOS code in Unity 2019, I've updated the code to reflect this.