【发布时间】:2020-01-09 02:07:32
【问题描述】:
我正在学习从在线制作测验应用程序,并且进展顺利。我想知道,与其从 assets 中读取 json ,不如从网上读取这样可以添加或相应更改问题并且用户不必更新 app.
这是 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;
}
我想在下一个问题加载时显示进度加载对话框,我们将不胜感激。提前致谢。
【问题讨论】:
-
下载 json 文本内容应该与下载任何其他文本上下文没有什么不同 - 你尝试过什么?