java - 如何从在线而不是资产文件夹中读取 Json
问题描述
我正在学习从在线制作测验应用程序,并且进展顺利。我想知道,与其从 assets 中读取 json ,不如从网上读取这样可以添加或相应更改问题并且用户不必更新 app.js 是明智的。
这是 JSON 结构。
{"questions" : [{"category":"general","question": "Grand Central Terminal, Park Avenue, New York is the world's", "choices": ["largest railway station","highest railway station","longest railway station","None of the above"], "correctAnswer":0},
{"category":"science","question": "Entomology is the science that studies", "choices": ["Behavior of human beings","Insects","The origin and history of technical and scientific terms","the formation of rocks"], "correctAnswer":1},
{"category":"science", "question":"What is known as the 'master gland' of the human body?", "choices":["Thyroid gland","Pituitary gland","Pineal gland","Pancreas"],"correctAnswer":1}
]}
从资产中读取的代码是
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = mContext.getAssets().open("questionsJSON.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
我想在下一个问题加载时显示进度加载对话框,任何帮助将不胜感激。提前致谢。
解决方案
推荐阅读
- amazon-web-services - Lambda 访问 EC2 实例上的 Kibana 服务以及 S3
- c# - 为什么 HashSet 保留插入顺序?
- coq - 外延性公理:为什么它不是不合理的
- ios - 如何从带有导航控制器 iOS 13 的模态呈现的 ViewController 中全屏推送 ViewController?
- javascript - 如何在JS中将声明变量简化为数组
- flutter - 如何在颤振中添加步进布局?
- msbuild - 在 Windows 容器内构建引用 master.dacpac 的 dacpac
- python - 我不明白我应该把我的 2 个输入放在这个 while 循环中的哪个位置
- angular - 如何在 jest puppeteer e2e 测试中运行单个测试文件?
- python - 熊猫数据根据条件变化