Back to project page LearnByHeart.
The source code is released under:
Apache License
If you think the Android project LearnByHeart listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.mps.learn.pb.http; /*from ww w . j a v a 2 s.c o m*/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Scanner; import java.util.TimeZone; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.res.TypedArray; import android.util.Log; import com.mps.learn.pb.App; import com.mps.learn.pb.R; import com.mps.learn.pb.model.PhraseBook; import com.mps.learn.pb.model.PhraseModel; public class PhraseParser { private static final String PHRASE_FILE_NAME = "phrases_pb_en_v1.json"; public static final String PREFERENCES_PHRASE= "phrasePreferences"; private static final String UPDATE_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'"; private static final long ONE_DAY_IN_MILLIS = 24 * 60 *60 * 1000; private SimpleDateFormat sdfUpdate; public PhraseParser(){ sdfUpdate = new SimpleDateFormat(UPDATE_DATE_FORMAT, Locale.getDefault()); sdfUpdate.setTimeZone(TimeZone.getTimeZone("UTC")); } public void downloadAndSaveIfNecessary(){ Log.d(getClass().getSimpleName(),"downloadAndSaveIfNecessary() : " + App.INSTANCE.getFilesDir()); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(App.INSTANCE.getString(R.string.url_json_pb_en)); try{ SharedPreferences preferences = App.INSTANCE.getSharedPreferences(PREFERENCES_PHRASE, Context.MODE_PRIVATE); String lastUpdate = preferences.getString(HttpConstant.Phrasebook.KEY_UPDATED_AT, null); if(isDateMoreThan1DayOld(lastUpdate)) { HttpResponse response = client.execute(httpGet); InputStream is = response.getEntity().getContent(); String content = new Scanner(is,"UTF-8").useDelimiter("\\A").next(); Log.d(getClass().getSimpleName(),"downloadAndSaveIfNecessary() : " + content ); File phraseFile = new File(App.INSTANCE.getFilesDir() , PHRASE_FILE_NAME); if(!phraseFile.exists()) phraseFile.createNewFile(); OutputStream fos = new FileOutputStream(phraseFile); fos.write(content.getBytes()); fos.flush(); fos.close(); } } catch(Exception e){ e.printStackTrace(); Log.e(getClass().getName(), "Unable to download JSON"); } } private void updateLastUpdateDate(String updatedAt){ Log.d(getClass().getSimpleName(),"updateLastUpdateDate()"); Editor editor = App.INSTANCE.getSharedPreferences(PREFERENCES_PHRASE, Context.MODE_PRIVATE).edit(); editor.putString(HttpConstant.Phrasebook.KEY_UPDATED_AT, updatedAt ); editor.commit(); } private boolean isDateMoreThan1DayOld (String stringDate){ Log.d(getClass().getSimpleName(),"isDateMoreThan1DayOld()"); try{ Date date = sdfUpdate.parse(stringDate); Date now = new Date(); Date yesterday = new Date (now.getTime() - ONE_DAY_IN_MILLIS); return date == null || date.getTime() < yesterday.getTime(); } catch(Exception e){ return true; } } public PhraseBook parseSavedPhrase(){ Log.d(getClass().getSimpleName(),"parseSavedPhrase()"); PhraseBook phraseBook = null; File phraseFile = new File(App.INSTANCE.getFilesDir() ,PHRASE_FILE_NAME); TypedArray images = App.INSTANCE.getResources().obtainTypedArray(R.array.background_color_images); if(phraseFile.exists()){ try{ InputStream is = new FileInputStream(phraseFile); JSONObject jsonCore = new JSONObject(new Scanner(is,"UTF-8").useDelimiter("\\A").next()); is.close(); JSONObject jsonObject = jsonCore.getJSONObject("phrasebook"); phraseBook = new PhraseBook(); Log.d(getClass().getSimpleName(),"parseSavedPhrase() : json : " + jsonObject.getString(HttpConstant.Phrasebook.KEY_HEADER)); phraseBook.setHeader(jsonObject.getString(HttpConstant.Phrasebook.KEY_HEADER)); phraseBook.setCategory(jsonObject.getString(HttpConstant.Phrasebook.KEY_CATEGORY)); phraseBook.setSubCategory(jsonObject.getString(HttpConstant.Phrasebook.KEY_SUM_CATEGORY)); phraseBook.setLangTranslatedFrom(jsonObject.getString(HttpConstant.Phrasebook.KEY_LANG_FROM)); phraseBook.setLangTranslatedTo(jsonObject.getString(HttpConstant.Phrasebook.KEY_LANG_TO)); phraseBook.setUpdatedAt(jsonObject.getString(HttpConstant.Phrasebook.KEY_UPDATED_AT)); phraseBook.setUpdatedBy(jsonObject.getString(HttpConstant.Phrasebook.KEY_UPDATED_BY)); JSONArray data = jsonObject.getJSONArray("phrases"); for (int i = 0; i < data.length(); i++) { JSONObject jsonItem = data.getJSONObject(i); PhraseModel phrase = new PhraseModel(); phrase.setTitle(jsonItem.getString(HttpConstant.Phrase.KEY_TITLE)); phrase.setResourceUrl(jsonItem.getString(HttpConstant.Phrase.KEY_IMAGE_URL)); phrase.setUsageEnglish(jsonItem.getString(HttpConstant.Phrase.KEY_PHRASE_EN)); phrase.setUsagePunjabi(jsonItem.getString(HttpConstant.Phrase.KEY_PHRASE_PB)); phrase.setImageId(images.getResourceId(i % 10 , -1)); phraseBook.getPhraseList().add(phrase); } // update the timestamp updateLastUpdateDate(phraseBook.getUpdatedAt()); } catch(Exception e){ Log.e(getClass().getName(), "Unable to parse saved phrases"); e.printStackTrace(); } } return phraseBook; } }