神盾局特工第三季13,校运会解说词,南京冯亚军
有时候我们需要通过自己的缓存机制来缓存网页内容,当没有网的时候显示本地的缓存,当有网的时候取最新的继续缓存到本地。
主要机制:
通过asynctask异步asynctask请求,将得到的response.body()缓存起来。
主要代码如下:
//通过okhttpclient加载html的方式先判断网页是否能走通 class webviewstatusrequester extends asynctask<string, string, integer> { string url; @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onpostexecute(integer result) { super.onpostexecute(result); result = 0; log.e(tag, "onpostexecute: result="+result +";url="+url); if(result == 1) { m_commonissuewebview.loadurl(url); } else if(result == 0) { getcachehtmlstrategy(); } } @override protected integer doinbackground(string... params) { url = params[0]; try { okhttpclient client = new okhttpclient(); request request = new request.builder().url(url).build(); response response = client.newcall(request).execute(); //缓存到本地文件 fileutil.writefile(pathconfig.getqahtmlcachepath(getactivity()),response.body().string()); if(response.issuccessful()) { return 1; } else { log.i("tag", "fail code:" + response.code()); return 0; } } catch (ioexception e) { e.printstacktrace(); } return 0; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论