今天我们来一起学习一下缓存技术,相信大家做开发的时候都知道请求网络数据的重要,但是有一些只用请求一次就过时性的消息比如某些新闻信息,如果我们每次进入新闻界面就从新从网络上获取势必会给用户带来不好的体验,所以我们需要缓存技术来帮我们解决这一问题。
我们就简单的取了前五条数据用来模拟我们的新闻,用的是热点热词的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 public void onItemClick(AdapterView<!--?--> arg0, View arg1, int arg2, long arg3) { // 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 ); } } public void intentNewsInfo( int arg2, String message, Bitmap bitmap) { Intent intent = new Intent(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 public class NewsinfoActivity extends Activity { private String NEWS_INFO = http: //api.yi18.net/top/show?id=; private String imageRes[] = { http: //d.hiphotos.baidu.com/image/h=360/sign=405b763459afa40f23c6c8db9b65038c/562c11dfa9ec8a13508c96e6f403918fa0ecc026.jpg, http: //c.hiphotos.baidu.com/image/h=360/sign=798b4f82caea15ce5eeee60f86013a25/9c16fdfaaf51f3dece3f986397eef01f3a297923.jpg, http: //f.hiphotos.baidu.com/image/h=360/sign=20a94e03940a304e4d22a6fce1c9a7c3/ac4bd11373f082028719ab3848fbfbedab641b29.jpg, http: //b.hiphotos.baidu.com/image/h=360/sign=3a1af7349145d688bc02b4a294c37dab/4b90f603738da977c0f5b82cb351f8198718e3db.jpg, http: //d.hiphotos.baidu.com/image/h=360/sign=75e596560f33874483c5297a610ed937/55e736d12f2eb9381891b2f4d6628535e5dd6f3c.jpg }; private Intent intent; private Util util; private int newId, index; private ImageView imageView; private TextView textView; private Bitmap bitmap; private String message; protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_newsinfo); intent = getIntent(); util = new Util(); 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 { new DownLoadJson().execute(NEWS_INFO + newId); new DownLoadBitmap().execute(imageRes[index]); Toast.makeText( this , 访问网络哦, 2000 ).show(); } } @Override public void onBackPressed() { Intent dataIntent = new Intent(); dataIntent.putExtra(message, message); dataIntent.putExtra(bitmap, bitmap); dataIntent.putExtra(newId, newId); setResult( 20 , dataIntent); finish(); super .onBackPressed(); } private void getJsonData(String json) { try { JSONObject jsonObject = new JSONObject(json); if (jsonObject.getBoolean(success)) { JSONObject jsonObject2 = new JSONObject( jsonObject.getString(yi18)); message = jsonObject2.getString(message); textView.setText(Html.fromHtml(jsonObject2.getString(message))); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } class DownLoadJson extends AsyncTask<string, string= "" > { @Override protected String doInBackground(String... params) { return util.downLoadJson(params[ 0 ]); } @Override protected void onPostExecute(String result) { if (result != null ) { getJsonData(result); } } } class DownLoadBitmap extends AsyncTask<string, bitmap= "" > { @Override protected Bitmap doInBackground(String... params) { // TODO Auto-generated method stub return util.downLoadBitmap(params[ 0 ]); } @Override protected void onPostExecute(Bitmap result) { if (result != null ) { bitmap = result; imageView.setImageBitmap(result); } } } }</string,></string,> 这就比较清晰明白了,每次我们都把这个界面获取到的信息存到LruCache里面。
? 1 2 3 4 5 6 7 8 9 @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { int newId = 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); } 4,效果
