我有一个.json本地文件,我不想把它放到服务器上,我只是想把它包含在我的app中。我试着直接把它贴到eclipse我的工程中,但是我得到一个异常:FileNotFoundException,我也试着把它贴到windows Explorer/Finder的工作区文件夹中,也得到了同样的异常信息。我应该把它放哪?
谢谢
你应该把文件夹直接放在你android工程的 /assets
或者是/res/raw
里。你可以用Context.getResources().openRawResource(R.raw.filename)
或者是
Context.getResources().getAssets().open("filename")
检索
把json文件夹放在assets文件夹中,我用的是这个方法
public static String jsonToStringFromAssetFolder(String fileName,Context context) throws IOException {
AssetManager manager = context.getAssets();
InputStream file = manager.open(fileName);
byte[] data = new byte[file.available()];
file.read(data);
file.close();
return new String(data);
}
当解析的时候我们可以用这个方法:
String jsondata= jsonToStringFromAssetFolder(your_filename, your_context);
jsonFileToJavaObjectsParsing(jsondata);