Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 13    Views: 337

krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
04/14/15 05:02 AM (9 years ago)

Android Interactive Quiz plugin - using local json file for quiz questions

Hi all I'm developing a new quiz app that will have at least 10 quiz screens. I have set up external JSON files (.txt files) and I'm using the dataURL setting in the control panel to download the JSON data for each quiz from the BT Control Panel. This works fine, but if internet connection is lost between say Quiz 1 and Quiz 2, then the user can't download the JSON file for Quiz 2. So ... is there a way to access JSON data stored from within the app's assets folder instead? e.g. assets > JSON > quiz1.txt assets > JSON > quiz2.txt ... I've tried changing the dataURL in the BT Control Panel to assets/JSON/quiz1.txt etc but this hopeful idea didn't work. Any ideas or workarounds? The key sections of plugin code are approx line 575: if(dataURL.length() > 1){ downloadScreenDataWorker = new DownloadScreenDataWorker(); downloadScreenDataWorker.setDownloadURL(dataURL); downloadScreenDataWorker.setSaveAsFileName(saveAsFileName); downloadScreenDataWorker.setThreadRunning(true); downloadScreenDataWorker.start(); }else{ BT_debugger.showIt(fragmentName + ":refreshScreenData NO DATA URL for this screen? Not downloading."); } } and approx line 383: //onResume... @Override public void onResume() { //BT_debugger.showIt(fragmentName + ":onResume"); super.onResume(); if(saveAsFileName.length() > 1){ //check cache... String parseData = ""; if(BT_fileManager.doesCachedFileExist(saveAsFileName)){ BT_debugger.showIt(fragmentName + ":onStart using cached screen data"); parseData = BT_fileManager.readTextFileFromCache(saveAsFileName); parseScreenData(parseData); }else{ //get data from URL if we have one... if(this.dataURL.length() > 1){ BT_debugger.showIt(fragmentName + ":onStart downloading screen data from URL"); refreshScreenData(); }else{ //parse with "empty" data... BT_debugger.showIt(fragmentName + ":onStart using data from app's configuration file"); parseScreenData(""); } } }//saveAsFileName }
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
04/14/15 05:39 AM (9 years ago)
Read local files from parseScreenData.
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
04/14/15 06:22 AM (9 years ago)
Hi Dusko Sorry, could you expand a bit further ... this is the first time I have used parse? If I have file called quiz2.txt in Assets > JSON folder - how would I parse this file? Is it a simple one liner? Many thanks
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
04/14/15 06:41 AM (9 years ago)
Hi there There is a site called parse.com, and we are not talking about it here. Parsing in computerese means going from one input data to another and checking whether each of the data contains some value. Now, each plugin for BT contains hundreds of lines and at first glance, no one but David Book will be able to understand it. However, the logic is one and the same throughout similar plugins, for instance, all plugins that contain field dataURL will contain similar code for working with dataURL. This logic is the same both on Android and on iOS, subject, naturally, differences in languages and platforms. Method parseScreenData is central, and please feel free to have a look at it on your own. It parses the data from the control panel for the plugin and that's the place to replace the data from the control panel to some kind of your own input data. For instance, if you know about JSON and want the plugin to read your own JSON instead of that that is coming from the control panel, then substitute JSON data in parseScreenData and the rest of the code will not even notice that the data are not coming from the expected source. Technically, it is not exactly easy to dive into the code and replace one JSON with another, but it can be done and I have done it many, many times so far. In your case, since you want to read data from 10 outside files instead of just one, some new code must be written. It should repeat the procedure that is already in place for reading one data URL, only 10 times or so. You will also have to make room, so to speak, for all those data in the code, so that you can find it and read it to show on the device. This is your chance to learn about the inner workings of Buzztouch, or to surrender and hire someone to do it for you.
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
04/14/15 06:57 AM (9 years ago)
For clarification - I am struggling to parse data from a single JSON file per quiz. I'm not looking to parse 10 files at the same time. I should be able to handle which set of JSON data is required through some kind logic test. But, it is to parse data from a single local file that is stumping me.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
04/14/15 07:36 AM (9 years ago)
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
04/14/15 09:03 AM (9 years ago)
Thanks Alan - I managed to get it working. Then changed something really minor and now it doesn't work. I'll keep working on it, but pretty sure this post will get me there. If I can't get it to work then I'll contact Smug and report back. Smug - thank you - again!! p.s. as Smug's code is written for an older version of BT, the activityName needs to be replaced by fragmentName
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/14/15 04:31 PM (9 years ago)
Wow. I can help while sleeping. Good to know. If you still have issues, gimme a yell; I'm awake now. Cheers! -- Smug
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
04/15/15 03:34 PM (9 years ago)
> Wow. I can help while sleeping. Good to know. That's when you know you're good. I've sent you a PM Smug as I'm still encountering problems. I'd be grateful if you could take a look.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/15/15 06:56 PM (9 years ago)
I sent you a reply, but I'll also answer here, in case it can help someone else, or if someone else has a better idea. I actually use this code in a few plugins now; it's an easier way for me, and a little more efficient than the old code I used to use: Basically, what it does is 'test' for a URL. If the dataURL is over a certain length but does not contain 'http', then it considers it a local file. dataURL = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "dataURL", ""); if ((dataURL.contains("http"))||(dataURL.length() < 3)) { // if it contains a URL, or if it's empty... saveAsFileName = this.screenData.getItemId() + "_screenData.txt"; } else { // it does not contain a URL, and it is not empty. Hope it's a childItem file... saveAsFileName = dataURL; BT_fileManager.copyAssetToCache("BT_Docs", dataURL); } This is placed in the onCreateView method, when the BT Properties are being populated. Hope this helps! Cheers! -- Smug
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
04/15/15 09:46 PM (9 years ago)
copyAssetToCache ... Wow, great find! We need to get those APIs documented in a hurry! :-)
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
04/16/15 02:58 AM (9 years ago)
Thanks for this Smug. I have sent you a new PM tho :o(
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
04/17/15 02:44 PM (9 years ago)
Thanks Smug all sorted. Sweet bit of code there. I have put together a little HOW TO for others: https://www.dropbox.com/s/4warok4o5z4vddb/Android%20-%20HOW%20TO%20use%20a%20locally%20stored%20Interactive%20Quiz%20question%20file.pdf?dl=0
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/17/15 04:54 PM (9 years ago)
Nicely done! Glad it's all working for you. And this method can pretty much work for almost any BT Plugin, not just the Quiz... Cheers! -- Smug
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.