简体   繁体   中英

C# Unity3D JSON Hanging

Firstly I must point out that I am very new to C#. I am developing an application using Unity3D, and part of the application requires that I parse a JSON file stored on my server.

The problem that I am having is that sometimes everything works perfectly, and other times the app hangs on the downloading the JSON. I don't receive any errors, the script just never reaches 100% on the progress.

Here is my code:

public IEnumerator DownloadJSONFile(string url) 
{
        Debug.Log("JSON URL: "+ url);
        mJsonInfo = new WWW(url);
        yield return mJsonInfo;

        mIsJSONRequested = true;
 }
private void LoadJSONData(string jsonUrl)
{

    Debug.LogWarning("LoadJSONData, url= "+jsonUrl);

    if(!mIsJSONRequested){

        StartCoroutine(DownloadJSONFile(jsonUrl));

    } else {

        if(mJsonInfo.progress >= 1)
        {


            if(mJsonInfo.error == null )
            {

                //** PARSE THE JSON HERE **//

            }else
            {
                Debug.LogError("Error downloading JSON");
                mIsLoadingData = false;
            }


        } else {
            Debug.LogWarning("!! ### JSON DOWNLOADING: "+mJsonInfo.progress+"%");
            if(mJsonInfo.error != null )
                {
                    Debug.LogError("Error downloading JSON");
                    Debug.LogError("JSON Error:"+mJsonInfo.error);
                    mIsLoadingData   = false;
                }
        }
    }
}

Like I said, 50% of the time the JSON data gets loaded nearly instantly, and 50% of the time the progress never reaches 1. I never receive an error in form the mJsonInfo.error variable.

Any suggestions as to what I am doing wrong would be greatly appreciated!

You need to wait until the download is complete.

As stated in the documentation you need to:

var www = new WWW(...);
yield return www;

So you need to modify the return type of your method from void to IEnumerator .

private IEnumerator LoadJSONData(string jsonUrl)
{
    Debug.LogWarning("LoadJSONData, url= "+jsonUrl);

    if(!mIsJSONRequested)
    {
        // Gets the json book info from the url
        mJsonInfo = new WWW(jsonUrl);
        yield return mJsonInfo; //Wait for download to complete
        mIsJSONRequested = true;
    } 
    else 
    {
      ...
    }
}

isDone is that you need,

WWW lWWW = new WWW(...)
if(lWWW.isDone)
 then parse it

I found the problem, I thought I would post my solution for anyone else experiencing the same problem. The solution in the end was the JSON file on the server. I was using PHP (CakePHP) to generate the JSON, when opening the PHP generated file via the browser the response time was instant, but from my mobile app for some reason it would hang. So I changed my server side code to actually create and updated an actual JSON file, and now everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM