[英]How to retrieve the downloaded data for the offline access in android?
我已經制作了一個應用程序,現在我想添加多項選擇題(接近 2000 年)的功能,該功能可用於離線訪問。 到目前為止,我一直認為是將文本文件上傳到 Google 的驅動器並獲取下載鏈接,一旦用戶下載該文本文件,它將被保存在內部存儲中,然后獲取所需的數據,如問題、選項等。 我避免使用 SQL,因為它會增加我的應用程序大小。請幫助! 提前致謝!
您可以使用SharedPreferences來存儲您的問題和選項。 只需將問題和答案保存為JSON格式即可。 在檢索時,您可以使用GSON將該JSON格式轉換為您的模型類。 這將幫助您更好地處理數據。
像這樣你可以使用:
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;
}
如果你想獲取問題和選項格式,你應該使用JSON格式保存問題和選項,將每個問題和選項作為 JSONOBJECT 放在 JSONARRAY 中。 所以你可以很容易地檢索它
然后您需要使用以下方法從您的 sdcard 中獲取下載的文件
//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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.