0

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.

Here is the JSON Structure.

{"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.

Pro Academy
  • 191
  • 1
  • 2
  • 12
  • 1
    Downloading json text content should be no different to downloading any other text context - what have you tried? – Scary Wombat Jan 09 '20 at 02:11

2 Answers2

3

Best option will be using REST APIs, Get data from Server/Database, which can be edited anytime from anywhere

You can learn to use Node js, it is not hard and it is based on JavaScript.

For getting JSON from APIs you can use 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!

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
1

Maybe consider using two different threads (or Runnables), one thread for downloading the JSON content and the other thread for displaying the GUI. For example, take a look at this: 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. 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. I suppose you can do something similar here by using a while loop, and then checking if the download is finished. If so, you can close the display.

Brandon Li
  • 55
  • 8