前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >6.请求网络步骤

6.请求网络步骤

作者头像
六月的雨
发布2018-05-14 11:32:36
6990
发布2018-05-14 11:32:36
举报
文章被收录于专栏:Android开发指南Android开发指南

操作步骤都是:加载本地数据——如果没有请求服务器——服务器请求完保存数据——本地数据有了或者保存完数据了去解析数据

FileUtils

代码语言:javascript
复制
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;		}	}}

BaseProtocol (协议) 

代码语言:javascript
复制
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请求访问的类,以后可以直接拿来用,比较方便

HttpHelper里是访问的主要代码

HttpRetry里是返回的结果

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-11-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档