Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Decoding HTML Entities

Discussion in 'Scripting' started by GladGoblinGames, Mar 25, 2018.

  1. GladGoblinGames

    GladGoblinGames

    Joined:
    Dec 14, 2011
    Posts:
    118
    Hello,

    I've been searching for a while now on how to decode HTML entities. I was originally using System.Web.dll using the HTTPUtility. However, I've now discovered this can't be used on Android (the build always fails)

    I've since tried searching for another way to accomplish this within Unity. I moved from using 'WWW' to 'UnityWebRequest' to see if there were any methods that I could use. However, still no joy. I've tried setting the request headers to 'text/plain' but I still can't get it to work. Please see the code below...

    Code (CSharp):
    1.         url = "https://opentdb.com/api.php?amount=" + 10 + "&difficulty=medium&category=31";
    2.         test = new UnityWebRequest(url);
    3.         test.downloadHandler = new DownloadHandlerBuffer();
    4.         test.SetRequestHeader("Content-Type", "text/plain");
    5.         yield return test.SendWebRequest();
    6.  
    7.         if(test.isNetworkError || test.isHttpError)
    8.         {
    9.             Debug.Log(test.error);
    10.         } else
    11.         {
    12.             Debug.Log(test.GetRequestHeader("Content-Type"));
    13.             Debug.Log(test.downloadHandler.text);
    14.         }
    Here you can see the &quot is not being decoded...
    Does anyone know of a way I can do this and still build my game for Android?

    Thanks
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    It might be helpful to know what the error on android was.
     
  3. GladGoblinGames

    GladGoblinGames

    Joined:
    Dec 14, 2011
    Posts:
    118
    There was no error on Android? As mentioned in my first post, the build fails saying that System.Web.dll is 'not allowed'
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
  5. GladGoblinGames

    GladGoblinGames

    Joined:
    Dec 14, 2011
    Posts:
    118
    I could have sworn I tried this before...oh well, seems to be working now! Thank you! :D
     
  6. GladGoblinGames

    GladGoblinGames

    Joined:
    Dec 14, 2011
    Posts:
    118
    Update: Turns out it didn't work, I am sorry to say.

    I'll paste what I have below.
    Code (CSharp):
    1.         jsonData = JsonMapper.ToObject(WWW.UnEscapeURL(www.text));
    2.         Debug.Log("Json Mapper...");
    3.         Debug.Log(WWW.UnEscapeURL(www.text));
    www.text still contains HTML entities.
     
  7. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Have you tried the WebUtility.HtmlDecode(string)?
     
  9. GladGoblinGames

    GladGoblinGames

    Joined:
    Dec 14, 2011
    Posts:
    118
    Hey, thanks for the replies both of you.

    Looks like Brathnann's solution worked. I've heard of the WebUtility before but I was looking for 'HttpUtility.HtmlDecode' which Unity didn't seem to have.

    Well, looks like its all sorted now. I just tried to export to Android, did an install and seems to be working just fine. Thanks all! :D