Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

JSON Decision Tree and Updater

nspaul edited this page Apr 3, 2013 · 4 revisions

With the Android version of the PTT Advisor, we opted to use a JSON file to define the decision tree. It lives in /assets/DTNode.json. We chose this format for a few reasons. Firstly, it's a lot easier to read and change than the hard-coded node structure in the existing iOS code. Secondly, we wanted to design the app from the start as generalizable. The meat of the app can be reused for any generic question/answer app. Change the .json file and poof you could be diagnosing schistosomiasis instead of blood clotting.

Its format is pretty straight forward. There's an array of "node" objects with these properties:

  • id: A unique integer identifier. Analogous to a key in a relational database. Node 0 is the first node.
  • question: The question text to display for this node
  • answers: An array of answer objects, which consist of:
    • nodeId: the id of the node this answer will take the user to
    • answer: the text of the answer (usually "Yes" or "No", but also "Continue", "Male", "Female"
  • footnotes: An array of strings, where each string is a separate footnote for this node
  • image: The header image to display for this node. It is the filename of an image in /res/drawable-hdpi without an extension.

Another benefit to this approach is that it translates to a relational database pretty seamlessly. We envisioned as a future project for another MIT team maybe creating a web interface for creating/modifying this flow chart. In theory it wouldn't be very hard to have javascript/html5 editor in a browser which creates and stores the decision tree in a database-- which, in turn, can export a .json file for the PTT Advisor to pull down for self-updating.

Speaking of self-updating, we went ahead and wrote code to handle this, should the CDC chose to use it it. In MainView.java's initialize() function, there is a this line: new JsonUpdaterTask(this).execute();

This creates a JsonUpdaterTask thread (with a reference to the MainView that called it) which then sets off in the background to check the internet for a new version of the JSON file. At present, it simply looks at the the copy on the Github repository. This is probably not a good idea for production use, so we probably deliver this to the CDC with this functionality disabled, and they can chose to enable it later at their leisure. You can change the URL it checks by changing this line from JsonUpdaterTask.java: private String UPDATEURL = "https://raw.github.com/androidpttadvisor/androidpttadvisor/master/AndroidPTTAdvisor/assets/DTNode.json";

To enable or disable the updating functionality: The only required change is to comment out the line mentioned above from MainView.java's initialize() function. If new JsonUpdaterTask(this).execute(); is commented out, then the updater thread will never be executed. It is recommended that if you disable this function and intend to publish it to the Google Play store, you should also remove this line from AndroidManifest.xml: <uses-permission android:name="android.permission.INTERNET"></uses-permission>

This line tells the Android operating system and the Google Play store that this app requires internet permissions. This is the only permission that the app requires, and if the updater is disabled, it doesn't even need this one.

So, in short, to disable the updater:

  • Comment out the call to new JsonUpdaterTask(this).execute(); from MainView.java's initialize() function and
  • Remove the <uses-permission android:name="android.permission.INTERNET"></uses-permission> from the AndroidManifest.xml

To enable the updater, uncomment the line and add the permission to the manifest. And modify the URL it checks, because pulling from the code repository is probably a Bad Thing.

Clone this wiki locally