添加自定义资源/ json文件时的NativeScript

时间:2015-07-08 05:20:00

标签: nativescript

我正在创建一个针对Android的项目,在其中,我希望它带有一个.json文件,从中可以加载一些数据。

我将.json文件放入Android文件夹。当运行“ tns运行android --device 1 ”(这是我的物理设备)时,我得到:

-code-gen:
[mergemanifest] No changes in the AndroidManifest files.
 [echo] Handling aidl files...
 [aidl] No AIDL files to compile.
 [echo] ----------
 [echo] Handling RenderScript files...
 [echo] ----------
 [echo] Handling Resources...
 [aapt] Found new input file
 [aapt] Generating resource IDs...
 [aapt] invalid resource directory name: /Users/konrad/Desktop/NativeScript/hello-world/platforms/android/res res
 [aapt] invalid resource directory name: /Users/konrad/Desktop/NativeScript/hello-world/platforms/android/res small.json

BUILD FAILED
/usr/local/Cellar/android-sdk/24.3.3/tools/ant/build.xml:649: The following error occurred while executing this line:
/usr/local/Cellar/android-sdk/24.3.3/tools/ant/build.xml:694: null returned: 1

Total time: 0 seconds
Command ant failed with exit code 1

该文件名为 small.json

编辑:即使我删除了文件,问题仍然存在。

1 个答案:

答案 0 :(得分:0)

Android的属性是它限制了对文件系统某些部分的访问。在NativeScript for Android上有两种访问文件的方法:

1)JavaScript方式

var fs = require('file-system');
var documents = fs.knownFolders.documents();
var myFile = documents.getFile(filename);   

在文档中指定:https://github.com/NativeScript/docs/blob/master/ApiReference/file-system/HOW-TO.md

2)Android特定方式包括名为“json-simple”的第三方库,需要使用命令 tns library add android 附加。

var context = app.android.context;
var assets = context.getAssets();

var br = new java.io.BufferedReader(new java.io.InputStreamReader(assets.open(filename)));

var parser = new org.json.simple.parser.JSONParser();
try {
    parser.parse(br);
} catch (ex) {
    var javaEx = ex.nativeException;
    var msg = javaEx.getMessage();
    console.log("whops! : "+msg);

}