简体   繁体   English

如何从在线读取 Json 而不是资产文件夹

[英]How to read Json from online instead of asset folder

I was learning to make quiz app from online and it is going well.我正在学习从在线制作测验应用程序,并且进展顺利。 I was wondering instead of reading json from assets , it will be wise to read from online such that question can be added or changes accordingly and user don't have to update app.我想知道与其从 assets 读取 json ,不如从在线读取,这样可以添加或相应地更改问题并且用户不必更新应用程序。

Here is the JSON Structure.这是 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} ]}

and the code to read from assets is从资产中读取的代码是

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

I would like to show progress loading dialog when next question loads and any help will be largely appreciated.我想在下一个问题加载时显示进度加载对话框,任何帮助将不胜感激。 Thanks in advance.提前致谢。

Best option will be using REST APIs , Get data from Server/Database, which can be edited anytime from anywhere最好的选择是使用REST API ,从服务器/数据库获取数据,可以随时随地编辑

You can learn to use Node js , it is not hard and it is based on JavaScript.你可以学习使用Node js ,它并不难,而且它是基于 JavaScript 的。

For getting JSON from APIs you can use Retrofit要从 API 获取 JSON,您可以使用Retrofit

Learning and implementing these things will be a bit hard if you are beginner but it will be the best option for long run如果您是初学者,学习和实施这些东西会有点困难,但从长远来看,这将是最佳选择

hope this helped!希望这有帮助!

Maybe consider using two different threads (or Runnables), one thread for downloading the JSON content and the other thread for displaying the GUI.也许考虑使用两个不同的线程(或 Runnables),一个线程用于下载 JSON 内容,另一个线程用于显示 GUI。 For example, take a look at this: Stackoverflow Post例如,看看这个: Stackoverflow Post

The solution involved making a Runnable that would first start downloading the data from the online website and then update the current progress onto the GUI thread as it is downloading.该解决方案涉及制作一个 Runnable,它首先开始从在线网站下载数据,然后在下载时将当前进度更新到 GUI 线程上。 He uses the BufferedInputStream class so he can use a while loop to read the data in, update the number of bytes downloaded, get the current progress, and then display the results.他使用BufferedInputStream类,因此他可以使用 while 循环读取数据,更新下载的字节数,获取当前进度,然后显示结果。 I suppose you can do something similar here by using a while loop, and then checking if the download is finished.我想你可以通过使用 while 循环来做类似的事情,然后检查下载是否完成。 If so, you can close the display.如果是这样,您可以关闭显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM