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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Retreiving Json Data don't Work on Android Devices

Discussion in 'Scripting' started by Ginxx009, Feb 28, 2018.

  1. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    So here's my LocalizationManager.cs

    Code (CSharp):
    1.  IEnumerator Refresh_()
    2.     {
    3.         Debug.LogWarning("TODO: 로딩 팝업 on.");
    4.  
    5.         rh.eLanguage language = tzGlobal.Instance.OPTION.language;
    6.        
    7.         string json = StreetUtility.LoadJsonFromStreamingAssets("notice.json");
    8.         if (json != null)
    9.         {
    10.             // 필요한 부분만 분리.
    11.             LitJson.JsonData data = LitJson.JsonMapper.ToObject(json);
    12.             json = data[language.ToString()].ToJson();
    13.            
    14.             notice = LitJson.JsonMapper.ToObject<string[]>(json);
    15.         }
    16.  
    17.         string path = string.Format("{0}/{1}/language", rh.Const.LOCALIZATION_PATH, language);
    18.         json = StreetUtility.LoadJsonFromResources(path);
    19.         if (json != null)
    20.         {
    21.             // json 로드.
    22.             Dictionary<string, string> dic = LitJson.JsonMapper.ToObject<Dictionary<string, string>>(json);
    23.  
    24.             // dictionary 카피.
    25.             dic_localization_text = new Dictionary<eTextKey, string>();
    26.             eTextKey e;
    27.             for (int i = 0; i < dic.Count; i++)
    28.             {
    29.                 e = (eTextKey)i;
    30.                 dic_localization_text[e] = dic[e.ToString()];
    31.             }
    32.  
    33.             // 등록된 localize 함수 실행.
    34.             for (int i = 0; i < list_localize_method.Count; i++)
    35.             {
    36.                 complete = false;
    37.                 list_localize_method[i].Invoke();
    38.                 yield return new WaitUntil(() => complete);
    39.             }
    40.         }
    41.  
    42.         Debug.LogWarning("TODO: 로딩 팝업 off.");
    43.     }
    And here's my StreetUtility.cs

    Code (CSharp):
    1.  public static string LoadJsonFromStreamingAssets(string path_with_extention_under_streaming_assets_folder)
    2.     {
    3.         string json = null;
    4.         try
    5.         {
    6.             //Android Platform
    7. #if UNITY_ANDROID
    8.             string oriPath = Path.Combine(Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
    9.  
    10.             WWW reader = new WWW(oriPath);
    11.             while (!reader.isDone) { }
    12.             string realPath = Application.persistentDataPath + "/" + path_with_extention_under_streaming_assets_folder;
    13.             System.IO.File.WriteAllBytes(realPath, reader.bytes);
    14.  
    15.             path_with_extention_under_streaming_assets_folder = realPath;
    16.  
    17. #elif UNITY_IOS //IOS Platform
    18.            
    19. #elif UNITY_STANDALONE //PC Platform
    20.             string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
    21.             StreamReader reader = new StreamReader(full_path);
    22.             json = reader.ReadToEnd().Trim();
    23.             reader.Close();
    24.  
    25.             Debug.Log(json);
    26. #endif
    27.         }
    28.         catch (Exception e)
    29.         {
    30.             Debug.LogWarningFormat("Failed to Load.\n{0}\n{1}", e, path_with_extention_under_streaming_assets_folder);
    31.         }
    32.         return json;
    33.     }
    34.    
    and here is my .Json that is verified in https://jsonlint.com/ .

    {
    "EN": [
    "Cancel is available with Cancel button.",
    "If you succeed in a lottery, you can receive additional rewards.",
    "Round 2 and 3 are shorter than you think.",
    "Please check the homepage in Confirm Game Method and Change Member Information."
    ],
    "KR": [
    "Cancel 버튼으로 배팅 취소가 가능합니다.",
    "로또에 성공하면 추가적인 보상을 받을 수 있습니다.",
    "라운드 2와 3은 생각보다 시간이 짧습니다.",
    "게임 방법 확인 및 회원정보 변경은 홈페이지에서 확인해주세요."
    ],
    "CN": [
    "[中文] Cancel is available with Cancel button.",
    "[中文] If you succeed in a lottery, you can receive additional rewards.",
    "[中文] Round 2 and 3 are shorter than you think.",
    "[中文] Please check the homepage in Confirm Game Method and Change Member Information."
    ],
    "HK": [
    "[广东话] Cancel is available with Cancel button.",
    "[广东话] If you succeed in a lottery, you can receive additional rewards.",
    "[广东话] Round 2 and 3 are shorter than you think.",
    "[广东话] Please check the homepage in Confirm Game Method and Change Member Information."
    ]
    }

    My problem here is that it's not getting the file . Please someone please help me out with this one.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    It's not working because you aren't setting json = to the string you get in the Android section of your code, so when you return json, it returns null.
     
    Last edited: Mar 6, 2018
  3. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    What do you mean sir? i don't quite understand
     
  4. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    He means this line
    Code (CSharp):
    1. string json = null;
    2. ///....
    3. json = reader.ReadToEnd().Trim(); //THIS ONE
    It is on your PC/Standalone compile section, but it isn't on your Android section. So json will always be null.
     
  5. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    My PC/Standalone is working fine . So should i do it also on my #if UNITY_ANDROID ? like this?

    Code (CSharp):
    1. json = null;
    2. while(!reader.isDone){}
    3. json = reader.text;
     
  6. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Yeah
     
  7. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Thank you sir it's actually working now what i did is like this

    json = null;
    while(!reader.isDone){}
    json = reader.text.Trim();
     
    whileBreak likes this.