标签:
操作步骤都是:加载本地数据——如果没有请求服务器——服务器请求完保存数据——本地数据有了或者保存完数据了去解析数据
public class FileUtils {
public static final String CACHE = "cache";
public static final String ICON = "icon";
public static final String ROOT = "GooglePlay";
/**
* 获取图片的缓存的路径
*
* @return
*/
public static File getIconDir() {
return getDir(ICON);
}
/**
* 获取缓存路径
*
* @return
*/
public static File getCacheDir() {
return getDir(CACHE);
}
public static File getDir(String cache) {
StringBuilder path = new StringBuilder();
if (isSDAvailable()) {
path.append(Environment.getExternalStorageDirectory()
.getAbsolutePath());
path.append(File.separator);// ‘/‘
path.append(ROOT);// /mnt/sdcard/GooglePlay
path.append(File.separator);
path.append(cache);// /mnt/sdcard/GooglePlay/cache
} else {
File filesDir = UiUtils.getContext().getCacheDir(); // cache
// getFileDir
// file
path.append(filesDir.getAbsolutePath());// /data/data/com.itheima.googleplay/cache
path.append(File.separator);// /data/data/com.itheima.googleplay/cache/
path.append(cache);// /data/data/com.itheima.googleplay/cache/cache
}
File file = new File(path.toString());
if (!file.exists() || !file.isDirectory()) {
file.mkdirs();// 创建文件夹
}
return file;
}
private static boolean isSDAvailable() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
}
public abstract class BaseProtocol<T> {
public T load(int index) {
// 加载本地数据
String json = loadLocal(index);
if (json == null) {
// 请求服务器
json = loadServer(index);
if (json != null) {
saveLocal(json, index);
}
}
if (json != null) {
return paserJson(json);
} else {
return null;
}
}
private String loadServer(int index) {
HttpResult httpResult = HttpHelper.get(HttpHelper.URL +getKey()//请求网络,写xutils也行
+ "?index=" + index);
String json = httpResult.getString();
return json;
}
private void saveLocal(String json, int index) {
BufferedWriter bw = null;
try {
File dir=FileUtils.getCacheDir();
//在第一行写一个过期时间
File file = new File(dir, getKey()+"_" + index); // /mnt/sdcard/googlePlay/cache/home_0
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(System.currentTimeMillis() + 1000 * 100 + "");//如果数字过期了重新请求网络
bw.newLine();// 换行
bw.write(json);// 把整个json文件保存起来
bw.flush();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
IOUtils.closeQuietly(bw);//关流
}
}
private String loadLocal(int index) {
// 如果发现文件已经过期了 就不要再去复用缓存了
File dir=FileUtils.getCacheDir();// 获取缓存所在的文件夹
File file = new File(dir, getKey()+"_" + index);
try {
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
long outOfDate = Long.parseLong(br.readLine());
if(System.currentTimeMillis()>outOfDate){
return null;
}else{
String str=null;
String sw=new StringWriter();
while((str=br.readLine())!=null){
sw.write(str);
}
return sw.toString();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解析json
* @param json
* @return
*/
public abstract T paserJson(String json);
/**
* 说明了关键字
* @return
*/
public abstract String getKey();
}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4969213.html