Download file from WWW in android.

Hello. I’m struggling with the problem of downloading .json files on Android. Everything works fine on a PC, but nothing happens on Android. No errors, I even installed the console to view what’s going on, but everything seems fine. I use this script:

    IEnumerator DownloadFiles() {
       
        WWW www1 = new WWW(bellsTimeURL);
        yield return www1;
        System.IO.File.WriteAllText("storage/emulated/0/PresentRemider_Data/lesson_data" + "/" + bellsTimeSaveName, www1.text);

        WWW www2 = new WWW(lessonTimeURL);
        yield return www2;
        System.IO.File.WriteAllText("storage/emulated/0/PresentRemider_Data/lesson_data" + "/" + lessonTimeSaveName, www2.text);

        WWW www3 = new WWW(versionURL);
        yield return www3;
        System.IO.File.WriteAllText("storage/emulated/0/PresentRemider_Data/lesson_data" + "/" + versionSaveName, www3.text);
}

It is a project that downloads a timetable. With .json files, I can easily modify the plan every week for the whole classmates. I have already tried to add arguments to the manifest. I changed Install Location to “Prefer External”, Write Permission to “External (SDCard)” and still nothing … I have been working on it for 3 days and nothing works. I also tried to change the download path from “Application.persistentDataPath” to the android “storage / emulated / 0 /” The only thing that downloads is empty files without any data. Please help me because I’m already damn

You should be checking for errors in the download.
Unity - Scripting API: WWW.error And example of error checking.

But, also note that UnityWebRequest is replacing WWW, so you may want to consider swapping. However, if your files are being created, but they are empty, it’s possible you have an error that is being returned.

I gain:

Unknown Error
#0 0xcecc5378 (libunity.so) ? 0x42f378
#1 0xcef5e770 (libunity.so) ? 0xaf3078
#2 0xcef5e770 (libunity.so) ? 0x6c8770
#3 0xcef5e668 (libunity.so) ? 0x6c8668

Edit:
This only occurs on Android

You can’t write to your _Data folder on an Android device. Use Application.persistentDataPath
e.g.

        System.IO.File.WriteAllText(Path.Combine(Application.persistentDataPath,  bellsTimeSaveName), www1.text);

Also consider using UnityWebRequest instead of WWW as WWW is now obsolete

There is still the same problem “Unknown Error”. I’m already tired of this combination :frowning:

Edit: When i use UnityWebRequest i dont gain any errors but still files are empty :frowning:

UnityWebRequest timeBells = UnityWebRequest.Get(bellsTimeURL);
yield return timeBells.SendWebRequest();
        if(timeBells.isNetworkError && timeBells.isHttpError) {
            Debug.Log(timeBells.error);
        } else {
            System.IO.File.WriteAllBytes(Application.persistentDataPath + "/" + bellsTimeSaveName, timeBells.downloadHandler.data);
        }

If you open a web browser on your Android test device and go to the URLs that your code is trying to access, what do you get?

Syntax should be as below, check for either not both

if(timeBells.isNetworkError || timeBells.isHttpError)
1 Like

I did what you wrote, when the page loads with the phone gets a raw json file.

I did as you wrote and admittedly I see an error now, but it is the same as on www

Can you paste the actual URLs you are passing in? Maybe there is something wrong with them.

https://anonymousfiles.io/f/bellsTime.json

I have no idea what’s wrong… I tried to download a small zip file, but it’s the same :frowning:

You always get Unkown error?
Which Unity version are you using?

1 Like

Problem solved.

The culprit was the unity version. I used “Unity 2019.1.10f1”. When i compiled the application on the version “Unity 2019.2.9f1” and everything works! Thank you so much, if it wasn’t for your ideas I would probably give up.

Best wishes guys!