Android: what is the best way to store JSON data offline for the app in android?

前端 未结 3 839
心在旅途
心在旅途 2021-02-05 18:42

I want to implement an app, that will show the route number, the rider has boarded in google map. So to find the route number I need some information about the routes, which are

相关标签:
3条回答
  • 2021-02-05 18:48

    Basically, there are two places where raw stuff may be stored: assets/ and res/raw/ (raw resources). These have been discussed a lot, for example, see the links:

    The reason for Assets and Raw Resources in Android

    http://thedevelopersinfo.com/2009/11/27/using-files-as-raw-resources-in-android/

    http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders

    0 讨论(0)
  • 2021-02-05 18:52

    You store abitrary files that you like to package with your app in the assets folder

    https://developer.android.com/tools/projects/index.html

    main/assets/

    This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

    See this question on how to read from assets.

    0 讨论(0)
  • 2021-02-05 19:11

    Maybe you can save them using SharedPreferences. You can store a json object/array by doing:

    Saving json to SharedPreferences:

        PreferenceManager.getDefaultSharedPreferences(context).edit()
    .putString("theJson",jsonObject.toString()).apply();
    

    Getting the stored json:

    JsonObject jsonObject = PreferenceManager.
    getDefaultSharedPreferences(this).getString("theJson","");
    
    0 讨论(0)
提交回复
热议问题