一、下载须要耗时,因此要启动异步加载,首先下载文件;数据库
class Josntask extendsAsyncTask<String, Void, String>json
String string = params[0];缓存
is =connection.getInputStream(); 网络
int len=0;app
byte[] buffer=newbyte[1024];异步
while((len=is.read(buffer))!=-1){ide
arrayBuffer.append(buffer,0, len) }优化
String string=new String(arrayBuffer.toByteArray(), 0, arrayBuffer.length());//用来保存数据编码
return stringurl
二、将数据用Jsonobject解析,用对象封装DataItem,并保存到arraylist
JSONArray array=new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String pic = jsonObject.getString("pic");
String fname = jsonObject.getString("fname");
String sname = jsonObject.getString("sname");
String time = jsonObject.getString("time");
DataItem dataItem=new DataItem(pic, fname, sname, time);
arrayList.add(0,dataItem);
三、listview数据发生更变,这时同时适配器更新
adapter.notifyDataSetChanged();
四、启动异步加载,下载图片
String string = params[0];
URL url=new URL(string);
URLConnection connection =url.openConnection();
is =connection.getInputStream();
bitmap = BitmapFactory.decodeStream(is)p_w_picpathMap.put(string, bitmap);
五、判断若是图片有出如今屏幕才去更新图片,不然会产生跳变
int fs=listView.getFirstVisiblePosition();//第一行
int count=listView.getChildCount();//总行数
int ls=fs+count-1;//最后一行
if(position>=fs && position<=ls)
{
p_w_picpathView.setImageBitmap(result);
}
六、作二级缓存(目的是让用户打开界面,若是没有网络的话,能够看到前几天更新的数据)
首先界面一打开就从数据库中读取数据
Cursor cursor = mDB.query("datadb", null, null, null, null, null, null);
boolean hasData =cursor.moveToFirst();
while(hasData){
Stringfname = cursor.getString(cursor.getColumnIndex("fname"));
Stringsname = cursor.getString(cursor.getColumnIndex("sname"));
Stringtime = cursor.getString(cursor.getColumnIndex("time"));
Stringpic = cursor.getString(cursor.getColumnIndex("pic"));
DataItemdataItem=new DataItem(pic, fname, sname, time);
arrayList.add(dataItem);
hasData=cursor.moveToNext();
而后在onPostExecute也就是有网络的时候从网络上保存数据到数据库
ContentValues values=new ContentValues();
values.put("fname", dataItem.fname);
values.put("sname", dataItem.sname);
values.put("time", dataItem.time);
values.put("pic", dataItem.pic);
mDB.insert("datadb", null, values);
最后保存图片:
一开机首先查找数据库是有保存地址,若是有把图片转换成图片对象
Cursor cursor = mDB.query("datadb", null, "fname=?", new String[]{fname}, null, null, null);
boolean hasData = cursor.moveToFirst();
if(hasData)
{
String pic = cursor.getString(cursor.getColumnIndex("pic"));
if(pic!=null && !"".equals(pic))//判断图片地址是否为空
{
File file=new File(pic);
if(file.exists())
{
FileInputStream fis=new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
if(bitmap!=null)
{
p_w_picpathMap.put(params[0], bitmap);
return bitmap;
}
若是图片地址为空的话,就从网络上下载,而且保持到sdcard中,更新到数据库
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))//判断sdcard状态是否可用
URL url=new URL(string);
URLConnection connection = url.openConnection();
is = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
if(bitmap!=null)
{
p_w_picpathMap.put(string, bitmap);
String path = url.getPath();//获取图片路径
int lastIndexOf = path.lastIndexOf("/");//获取图片的名字
String filename = path.substring(lastIndexOf);
File sdcardfile= Environment.getExternalStorageDirectory();//获取sdcard地址
Filefile=new File(sdcardfile.getAbsolutePath()+"/p_w_picpath/"+filename);//拼接sdcard路径
File parentFile = file.getParentFile();//文件夹的路径
if(!parentFile.exists())
{
parentFile.mkdirs();//若是文件夹不存在就建立,第一保存不存在
}
FileOutputStream fos=new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);//将Bitmap压缩成JPEG编码,质量为100%存储
fos.flush();
update(file.getAbsolutePath(),fname);//更新数据库图片地址
最后须要作其余的优化,例如listview的复用,图片最好保存到hashmap就不须要每次要用的时候都从网络上下载,设置网络超时的时间等等。