0

In my # UNITY_STANDALONE this line of code can read my json file and i do it like this

Define_ConstantValue.cs

#if UNITY_STANDALONE
        string path = string.Format("{0}/datacenter.json", Application.streamingAssetsPath);
                     if (File.Exists(path)) {
                        StreamReader reader = new StreamReader(path);
                        try
                        {
                            CheckJSonManager.Instance._DataCenterJson = LitJson.JsonMapper.ToObject<DataCenterJson>(reader.ReadToEnd().Trim());
                            DataCenter_BaseURL = CheckJSonManager.Instance._DataCenterJson.dataCenter;
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError("path : " + path + "\n" + ex);
                        }
                        reader.Close();
                    }


                    // (Get dealer server connection information) 딜러서버 접속 정보를 가져온다. 
                    // (If the json file below is present, 'I am a dealer console) 아래 json파일이 존재하면, '나는 딜러콘솔이다'
                        path = string.Format("{0}/dealerserver.json", Application.streamingAssetsPath);
                        if (File.Exists(path))
                        {
                            StreamReader reader = new StreamReader(path);
                            try
                            {
                                Debug.Log("*********************   DealerServer.JSON   ***************************");
                                CheckJSonManager.Instance._DealerServerJson = LitJson.JsonMapper.ToObject<DealerServerJson>(reader.ReadToEnd().Trim());
                            }
                            catch (Exception ex)
                            {
                                Debug.LogError("path : " + path + "\n" + ex);
                            }
                            reader.Close();
                        }
                        else
                        {
                            CheckJSonManager.Instance._DealerServerJson = new DealerServerJson();
                        }

                        // (Server list URL) 서버리스트 URL.
                        serverListJsonURL = string.Format("{0}/pc_version/ph/check/serverList.json", DataCenter_BaseURL);
                        NOTICE_URL = string.Format("{0}/pc_version/ph/check/notice.json", DataCenter_BaseURL);
   #endif

And displaying it like this in my

LogoUI.cs

 // URL을 로드한다 ( Load the URL ).
    CheckJSonManager.Instance._AwsDatacenterURL.SetURL();

    // notice를 다운로드한다 ( download 'notice.json ).
    WWW www = new WWW(CheckJSonManager.Instance._AwsDatacenterURL.NOTICE_URL);
    yield return www;

    //Debug PK 12/13/2017
    Debug.Log("www notice url = " + CheckJSonManager.Instance._AwsDatacenterURL.NOTICE_URL);

    // notice를 저장한다. ( save 'notice.json' )
    StreetUtility.SaveJson_mk2(www.text, string.Format("{0}/notice.json", Application.streamingAssetsPath));

    // (Download server list) 서버 리스트를 다운로드한다.
    www = new WWW(CheckJSonManager.Instance._AwsDatacenterURL.SERVERLIST_URL);
    yield return www;

    Debug.Log("www serverlist url = " + CheckJSonManager.Instance._AwsDatacenterURL.SERVERLIST_URL);

Now this code returns a value of

www notice url = https://*********-***.amazonaws.com/pc_version/ph/check/notice.json

www serverlist url = https://*******-****.amazonaws.com/pc_version/ph/check/serverList.json

Now my problem occurs here in my # UNITY_ANDROID line of code because it can't read my the json file from my streamingasset path. here's my code:

#if UNITY_ANDROID

        string path = "jar:file://" + Application.dataPath + "!/assets/datacenter.json";
        //string dataAsJson;

            if (File.Exists(path)) {
                WWW reader = new WWW(path);
                while (!reader.isDone) ;
                string filePath = string.Format("{0}/{1}", Application.persistentDataPath, "datacenter.json");
                File.WriteAllBytes(filePath, reader.bytes);
            //byte[] dataAsBytes = reader.bytes;
            //dataAsJson = System.Text.Encoding.Default.GetString(dataAsBytes);

            StreamReader wr = new StreamReader(filePath);
            string line;
            while ((line = wr.ReadLine())!= null)
            {
                try
                {
                    CheckJSonManager.Instance._DataCenterJson = LitJson.JsonMapper.ToObject<DataCenterJson>(reader.text);
                    DataCenter_BaseURL = CheckJSonManager.Instance._DataCenterJson.dataCenter;
                }
                catch (Exception ex)
                {
                    Debug.LogError("Path : " + path + "\n" + ex);
                }
            }  
            }

            path = "jar:file://" + Application.dataPath + "!/assets/dealerserver.json";

            if (File.Exists(path))
            {
                WWW reader = new WWW(path);
                while (!reader.isDone) { }
                string filePath = string.Format("{0}/{1}", Application.persistentDataPath, "dealerserver.json");
                File.WriteAllBytes(filePath, reader.bytes);
            //byte[] dataAsBytes = reader.bytes;
            //dataAsJson = System.Text.Encoding.Default.GetString(dataAsBytes);

            StreamReader wr = new StreamReader(filePath);
            string line;
            while ((line = wr.ReadLine()) != null)
            { 
                try
                {
                    Debug.Log("*************************  DealerServer.JSON *************************");
                    CheckJSonManager.Instance._DealerServerJson = LitJson.JsonMapper.ToObject<DealerServerJson>(reader.text);
                }
                catch (Exception ex)
                {
                    Debug.LogError("Path : " + path + "\n" + ex);
                }

            }
            } else
            {
                CheckJSonManager.Instance._DealerServerJson = new DealerServerJson();
            }
        serverListJsonURL = string.Format("{0}/pc_version/ph/check/serverList.json", DataCenter_BaseURL);
        NOTICE_URL = string.Format("{0}/pc_version/ph/check/notice.json", DataCenter_BaseURL);
  #endif

and i call it the same on the #LogoUI.cs and it returns a value of

www serverlist url = /pc_version/ph/check/serverList.json

www notice url = /pc_version/ph/check/notice.json

I guess this line of code doesn't work on android?

serverListJsonURL = string.Format("{0}/pc_version/ph/check/serverList.json", DataCenter_BaseURL);
NOTICE_URL = string.Format("{0}/pc_version/ph/check/notice.json", DataCenter_BaseURL);

UPDATE :

When I build my project as .apk and debug it with logcat, I am not getting the same result as # UNITY_STANDALONE in # UNITY_ANDROID.

In my

# UNITY_STANDALONE

it can get this value

www notice url = https://*********-***.amazonaws.com/pc_version/ph/check/notice.json

www serverlist url = https://*******-****.amazonaws.com/pc_version/ph/check/serverList.json

while on my

# UNITY_ANDROID

it doesn't return my this value which is it is on my datacenter.json

www serverlist url = /pc_version/ph/check/serverList.json

www notice url = /pc_version/ph/check/notice.json

it should return the notice url and serverlist url a value like this

www notice url = https://*********-***.amazonaws.com/pc_version/ph/check/notice.json

www serverlist url = https://*******-****.amazonaws.com/pc_version/ph/check/serverList.json

halfer
  • 19,824
  • 17
  • 99
  • 186
NoobProgrammer
  • 488
  • 1
  • 8
  • 21
  • I struggle to understand your problem. What exact behaviour do you expect? Also, have you debugged your code to understand what happens line by line? On a side note: catching exceptions and not handling them is dangerous. Maybe that could be a first issue to address and maybe that even solves your problem. – Quality Catalyst Dec 15 '17 at 04:48
  • When i build my code as .apk and in my logcat the value i wanted is not happening. i'll edit my question – NoobProgrammer Dec 15 '17 at 05:00
  • I suggest you to debug line by line. And also make sure to understand how your network request behaves on the specific platform, which may be being executed asynchronously and you are just not waiting enough until you get the response from network. – viz Dec 15 '17 at 05:10
  • so is the WWW class not returning that fast?? @viz – NoobProgrammer Dec 15 '17 at 05:17
  • I'm not an expert on C# or Unity. You can head up to this qna to understand general flow of the WWW API: https://stackoverflow.com/questions/34301558/in-unity-is-a-www-class-asynchronous-or-synchronous Anyhow I recommend you to debug your code line by line with the platform support to understand where your code is going wrong. – viz Dec 15 '17 at 05:22
  • And make sure you have specified the required permission on the android manifest. See https://forum.unity.com/threads/android-simple-www-call-not-working.86952/ – viz Dec 15 '17 at 05:27
  • I've been struggling really on debugging this. – NoobProgrammer Dec 15 '17 at 05:28
  • See also: https://stackoverflow.com/questions/42762702/unity-www-text-returning-null-on-android-device Make sure you are specifying the protocol 'http' or 'https' explicitly. – viz Dec 15 '17 at 05:29
  • I noticed this line of code `StreetUtility.SaveJson_mk2(www.text, string.Format("{0}/notice.json", Application.streamingAssetsPath));` which looks like it is saving data to the `Application.streamingAssetsPath` path. For the 100th time, you can't write to the `Application.streamingAssetsPath` path. You can only read from it.The sooner you understand this, the better. – Programmer Dec 15 '17 at 05:59
  • so i'll comment it out sir? @Programmer cause on my `StreetUtility` i have this code `StreamWriter writer = new StreamWriter(path, false); writer.WriteLine(json); writer.Close();` – NoobProgrammer Dec 15 '17 at 06:08
  • I will reply in my other answer. Check there. – Programmer Dec 15 '17 at 06:09
  • Pro-tip: in your question text and in your comments, please do not plead - it will only serve to irritate readers and expose you to potential downvotes. "Please help me", "I am stuck for X days", and sad-face emoticons are all forms of begging. Keep it succinct, and if you get advice on how to solve a problem (e.g. that you need to do some debugging) then please take it. Once you have done that work and edited the question to show it, the question may be reopened at that point, if you still have a problem. Good luck! – halfer Dec 15 '17 at 17:11

0 Answers0