I have made an app and now I want to add feature of Multiple Choice Questions(near abt 2000)which will be available for offline access. What I have thought till now is to upload the text file to Google's drive and get the downloading link,Once the user downloads that text file it will be saved in Internal Storage and then fetch the required data like Questions,Options,etc. I avoided to use SQL since it will increase my app size.Kindly Help! Thanks in Advance!
Asked
Active
Viewed 486 times
-1
-
Whats the issue you are facing ? – Nitesh Jan 24 '17 at 07:08
-
use Firebase Realtime Database – sivaBE35 Jan 24 '17 at 07:11
-
1@siva35 I want to make it offline permanently once the data is downloaded.FIrebase Realtime Database need Internet Connection to fetch the data after restarting an app. – Shubh.J Jan 24 '17 at 07:15
-
@Nitesh I don't know,how to do that! – Shubh.J Jan 24 '17 at 07:16
2 Answers
1
You can use SharedPreferences for storing your question and Options. Just save the question and answer in a JSON format. While retriving you can use GSON for converting that JSON format into your Model class. This will help you in better handling of the data.
Like this you can use :
private SharedPreferences sharedPreference;
sharedPreference=context.getApplicationContext().getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
public void saveQuestionOptionResponse(String response) {
sharedPreference.edit().putString("Question", response);
sharedPreference.edit().commit();
}
public QuestionOptionModel getQuestionOption() {
Gson gson = new Gson();
String json = sharedPreference.getString("Question", "");
QuestionOptionModel model = gson.fromJson(json, QuestionOptionModel.class);
return response;
}

swetabh suman
- 1,949
- 2
- 19
- 24
-
So, I need to upload the MCQs text file to the Google's drive which will be having questions and answers in JSON format.Once it is downloaded to Internal Storage,I need to fetch that data and convert into GSON.Thus it will require the Internet Connection only at once.Am I right? – Shubh.J Jan 24 '17 at 07:46
-
-
Well,can you please tell how to write JSON data in text file or should I use any other format? – Shubh.J Jan 24 '17 at 07:52
-
You can google through it. Its really a very lightweight data-interchange format. – swetabh suman Jan 24 '17 at 07:56
-
Or one thing you can do is , instead of adding a text file to google drive you can create a text file within the app itself inside asset folder , and later you can fetch the content of the file and any time and display the data. It will not require any shared preference or any file storage. – swetabh suman Jan 24 '17 at 07:58
-
please follow this link to get to know how to load and fetch JSON file from asset : http://stackoverflow.com/questions/19945411/android-java-how-can-i-parse-a-local-json-file-from-assets-folder-into-a-listvi – swetabh suman Jan 24 '17 at 08:01
-
If I put near about 2000 questions in a text file and added it to Asset folder, I think It will increase the app size,Did't it? – Shubh.J Jan 24 '17 at 10:22
-
-
0
If you want to get the questions and options format, you should use to save the questions and options in a JSON format, put the each question and options as JSONOBJECT in a JSONARRAY. So you can retrive it easly
Then you need to fetch the downloaded file from your sdcard using the below method
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}

Aneesh P V
- 1,390
- 1
- 10
- 13
-
How to use JSON for offline access ?Like JSON is to communicate with server.Please debrief your answer. – Shubh.J Jan 24 '17 at 07:26
-
the content of the created file should placed in a json structure questions[ { q1:question, A:opt1, B:opt2, C:opt3 } ] using this format in your downloaded file, then we can easily access your data into app in offline – Aneesh P V Jan 24 '17 at 09:49