又是好久没写博客。。
今天我们来一起学习一下缓存技术,相信大家做开发的时候都知道请求网络数据的重要,但是有一些只用请求一次就过时性的消息比如某些新闻信息,如果我们每次进入新闻界面就从新从网络上获取势必会给用户带来不好的体验,所以我们需要缓存技术来帮我们解决这一问题。
/** * 缓存json数据 */ private LruCache<Integer, String> mJsonCache; /** * 缓存图片信息 */ private LruCache<Integer, Bitmap> mBitmapCache; public Util() { mJsonCache = new LruCache<Integer, String>(1 * 1024 * 1024); mBitmapCache = new LruCache<Integer, Bitmap>(2 * 1024 * 1024); } /** * 添加进入缓存列表 * * @param key * @param value */ public void addJsonLruCache(Integer key, String value) { mJsonCache.put(key, value); } public void addBitmapLruCache(Integer key, Bitmap value) { mBitmapCache.put(key, value); } /** * 从缓存列表中拿出来 * * @param key * @return */ public String getJsonLruCache(Integer key) { return mJsonCache.get(key); } public Bitmap getBitmapLruCache(Integer key) { return mBitmapCache.get(key); }
// 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。 // LruCache通过构造函数传入缓存值,以KB为单位。 int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);一般来说最大值的1/8左右就可以了。
public class MainActivity extends Activity implements OnItemClickListener { private static final String LIST_DATA = "http://api.yi18.net/top/list"; private ListView mListView; private ArrayAdapter<String> mAdapter; private ArrayList<Integer> mListId; private Util util; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); util = new Util(); mListView = (ListView) findViewById(R.id.list); mListId = new ArrayList<Integer>(); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(this); new DownLoadJson().execute(LIST_DATA); }这一段就是普通的请求数据添加到ListView中。
private void getJsonData(String json) { try { JSONObject jsonObject = new JSONObject(json); if (jsonObject.getBoolean("success")) { JSONArray jsonArray = jsonObject.getJSONArray("yi18"); for (int i = 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(); } } class DownLoadJson extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { return util.downLoadJson(params[0]); } @Override protected void onPostExecute(String result) { if (result != null) { getJsonData(result); } } }
我们就简单的取了前五条数据用来模拟我们的新闻,用的是热点热词的Api。
3,缓存
@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); }
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%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" }; 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, Void, 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, Void, 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); } } } }
@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); }
原文地址:http://blog.csdn.net/u014163726/article/details/43561001