0

我正在创建一个以 Android 为目标的项目,我希望它附带一个 .json 文件,从中加载一些数据。

我将 .json 文件放入 Android 文件夹。运行“ tns run 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

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

4

1 回答 1

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);

}
于 2015-07-09T10:08:50.500 回答