今天我们来一起学习一下缓存技术,相信大家做开发的时候都知道请求网络数据的重要,但是有一些只用请求一次就过时性的消息比如某些新闻信息,如果我们每次进入新闻界面就从新从网络上获取势必会给用户带来不好的体验,所以我们需要缓存技术来帮我们解决这一问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /** * 缓存json数据 */ privateLruCache<integer, string=""> mJsonCache; /** * 缓存图片信息 */ privateLruCache<integer, bitmap=""> mBitmapCache; publicUtil() { mJsonCache =newLruCache<integer, string="">(1*1024*1024); mBitmapCache =newLruCache<integer, bitmap="">(2*1024*1024); } /** * 添加进入缓存列表 * * @param key * @param value */ publicvoidaddJsonLruCache(Integer key, String value) { mJsonCache.put(key, value); } publicvoidaddBitmapLruCache(Integer key, Bitmap value) { mBitmapCache.put(key, value); } /** * 从缓存列表中拿出来 * * @param key * @return */ publicString getJsonLruCache(Integer key) { returnmJsonCache.get(key); } publicBitmap getBitmapLruCache(Integer key) { returnmBitmapCache.get(key); }</integer,></integer,></integer,></integer,> |
1 2 3 | // 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。 // LruCache通过构造函数传入缓存值,以KB为单位。 intmaxMemory = (int) (Runtime.getRuntime().maxMemory() /1024); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | publicclassMainActivityextendsActivityimplementsOnItemClickListener { privatestaticfinalString LIST_DATA = http://api.yi18.net/top/list; privateListView mListView; privateArrayAdapter<string> mAdapter; privateArrayList<integer> mListId; privateUtil util; protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); util =newUtil(); mListView = (ListView) findViewById(R.id.list); mListId =newArrayList<integer>(); mAdapter =newArrayAdapter<string>(this, android.R.layout.simple_list_item_1); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(this); newDownLoadJson().execute(LIST_DATA); }</string></integer></integer></string> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | privatevoidgetJsonData(String json) { try{ JSONObject jsonObject =newJSONObject(json); if(jsonObject.getBoolean(success)) { JSONArray jsonArray = jsonObject.getJSONArray(yi18); for(inti =0; i < jsonArray.length(); i++) { JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i); if(i <5) { mAdapter.add(jsonObject2.getString(title)); mListId.add(jsonObject2.getInt(id)); } } } }catch(JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } classDownLoadJsonextendsAsyncTask<string, string=""> { @Override protectedString doInBackground(String... params) { returnutil.downLoadJson(params[0]); } @Override protectedvoidonPostExecute(String result) { if(result !=null) { getJsonData(result); } } }</string,> |
我们就简单的取了前五条数据用来模拟我们的新闻,用的是热点热词的Api。
3,缓存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @Override publicvoidonItemClick(AdapterView<!--?--> arg0, View arg1,intarg2,longarg3) { // TODO Auto-generated method stub String message = util.getJsonLruCache(mListId.get(arg2)); Bitmap bitmap = util.getBitmapLruCache(mListId.get(arg2)); if(message !=null) { intentNewsInfo(arg2, message, bitmap); }else{ intentNewsInfo(arg2,null,null); } } publicvoidintentNewsInfo(intarg2, String message, Bitmap bitmap) { Intent intent =newIntent(MainActivity.this, NewsinfoActivity.class); intent.putExtra(message, message); intent.putExtra(bitmap, bitmap); intent.putExtra(index, arg2); intent.putExtra(id, mListId.get(arg2)); startActivityForResult(intent,100); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | publicclassNewsinfoActivityextendsActivity { privateString NEWS_INFO = http://api.yi18.net/top/show?id=; privateString imageRes[] = { http://d.hiphotos.baidu.com/image/h%3D360/sign=405b763459afa40f23c6c8db9b65038c/562c11dfa9ec8a13508c96e6f403918fa0ecc026.jpg, http://c.hiphotos.baidu.com/image/h%3D360/sign=798b4f82caea15ce5eeee60f86013a25/9c16fdfaaf51f3dece3f986397eef01f3a297923.jpg, http://f.hiphotos.baidu.com/image/h%3D360/sign=20a94e03940a304e4d22a6fce1c9a7c3/ac4bd11373f082028719ab3848fbfbedab641b29.jpg, http://b.hiphotos.baidu.com/image/h%3D360/sign=3a1af7349145d688bc02b4a294c37dab/4b90f603738da977c0f5b82cb351f8198718e3db.jpg, http://d.hiphotos.baidu.com/image/h%3D360/sign=75e596560f33874483c5297a610ed937/55e736d12f2eb9381891b2f4d6628535e5dd6f3c.jpg }; privateIntent intent; privateUtil util; privateintnewId, index; privateImageView imageView; privateTextView textView; privateBitmap bitmap; privateString message; protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_newsinfo); intent = getIntent(); util =newUtil(); imageView = (ImageView) findViewById(R.id.image); textView = (TextView) findViewById(R.id.message); newId = intent.getExtras().getInt(id); index = intent.getExtras().getInt(index); if(intent.getExtras().getString(message) !=null) { message = intent.getExtras().getString(message); bitmap = intent.getParcelableExtra(bitmap); textView.setText(Html.fromHtml(message)); imageView.setImageBitmap(bitmap); Toast.makeText(this, 没有访问网络哦,2000).show(); }else{ newDownLoadJson().execute(NEWS_INFO + newId); newDownLoadBitmap().execute(imageRes[index]); Toast.makeText(this, 访问网络哦,2000).show(); } } @Override publicvoidonBackPressed() { Intent dataIntent =newIntent(); dataIntent.putExtra(message, message); dataIntent.putExtra(bitmap, bitmap); dataIntent.putExtra(newId, newId); setResult(20, dataIntent); finish(); super.onBackPressed(); } privatevoidgetJsonData(String json) { try{ JSONObject jsonObject =newJSONObject(json); if(jsonObject.getBoolean(success)) { JSONObject jsonObject2 =newJSONObject( jsonObject.getString(yi18)); message = jsonObject2.getString(message); textView.setText(Html.fromHtml(jsonObject2.getString(message))); } }catch(JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } classDownLoadJsonextendsAsyncTask<string, string=""> { @Override protectedString doInBackground(String... params) { returnutil.downLoadJson(params[0]); } @Override protectedvoidonPostExecute(String result) { if(result !=null) { getJsonData(result); } } } classDownLoadBitmapextendsAsyncTask<string, bitmap=""> { @Override protectedBitmap doInBackground(String... params) { // TODO Auto-generated method stub returnutil.downLoadBitmap(params[0]); } @Override protectedvoidonPostExecute(Bitmap result) { if(result !=null) { bitmap = result; imageView.setImageBitmap(result); } } } }</string,></string,> |
1 2 3 4 5 6 7 8 9 | @Override protectedvoidonActivityResult(intrequestCode,intresultCode, Intent data) { intnewId = data.getExtras().getInt(newId); String message = data.getExtras().getString(message); Bitmap bitmap = data.getParcelableExtra(bitmap); util.addJsonLruCache(newId, message); util.addBitmapLruCache(newId, bitmap); super.onActivityResult(requestCode, resultCode, data); } |