博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AndroidWidget实践 --- EverydayTips开发(6)
阅读量:6987 次
发布时间:2019-06-27

本文共 1877 字,大约阅读时间需要 6 分钟。

hot3.png

这次实现了简单的从网络更新tips.txt

先检查是否有更新,使用的sae的空间

网址是txtfan.sinaapp.com

public static Boolean check(Context context, String path) throws Exception {		String date = "";		String formerdate = "";		SharedPreferences settings = context.getSharedPreferences("date",				Context.MODE_PRIVATE);		formerdate = settings.getString("data", null);// 读取之前的更新日期		HttpPost httpRequest = new HttpPost(path);		try {			// 取得HTTP response			HttpResponse httpResponse = new DefaultHttpClient()					.execute(httpRequest);			// 若状态码为200 ok			if (httpResponse.getStatusLine().getStatusCode() != 404) {				// 取出回应字串				date = EntityUtils.toString(httpResponse.getEntity());			} else {				date = "0";			}		} catch (ClientProtocolException e) {			date = "0";			e.printStackTrace();		} catch (UnsupportedEncodingException e) {			date = "0";			e.printStackTrace();		} catch (IOException e) {			date = "0";			e.printStackTrace();		}		SharedPreferences.Editor editor = settings.edit();// 保存现在的更新日期		editor.putString("data", date);		editor.commit();		if (!date.equals(formerdate)) {			return true;		} else {			return false;		}	}
有的话

public static Boolean getTipsOnline(String path) throws Exception {		File fileTip = new File(DATA_PATH + "tips.txt");		if (fileTip.exists()) {			fileTip.renameTo(new File(DATA_PATH + System.currentTimeMillis()					+ "bktips.txt"));		}		URL url = new URL(path);		HttpURLConnection conn = (HttpURLConnection) url.openConnection();		conn.setRequestMethod("GET");		conn.setConnectTimeout(5 * 1000);		InputStream inStream = conn.getInputStream();// 通过输入流获取html数据		byte[] data = StreamTool.readInputStream(inStream);// 得到html的二进制数据		DataOutputStream dataOutputStream = new DataOutputStream(				new FileOutputStream(DATA_PATH + "tips.txt"));		dataOutputStream.write(data);		dataOutputStream.flush();		return true;	}
http://dl.dbank.com/c0ez81qgts

转载于:https://my.oschina.net/sfshine/blog/470696

你可能感兴趣的文章
我的友情链接
查看>>
EDM资讯分享:Focussend应邀出席第五届出境自由行&定制游高峰论坛
查看>>
我的友情链接
查看>>
α冲刺 (6/10)
查看>>
Xcode7 低版本iOS系统上下有黑边的问题
查看>>
数据库查询集与反射的应用(自己写的小例子)
查看>>
关于exchange数据库无法装载问题分析处理
查看>>
nginx配置之一堆without
查看>>
iOS 系统架构及常用框架
查看>>
(毕业)上海行
查看>>
Nginx 源码学习资料
查看>>
Postfix 删除队列中的邮件
查看>>
我的友情链接
查看>>
GTK+Glade3 Gtk-WARNING **: Could not find signal handler 问题最终解析
查看>>
证书??
查看>>
JAVA兼职架构师
查看>>
Linux 进程和作业管理
查看>>
CSS布局标准
查看>>
Centos在VMware虚拟机上的网络配置一记
查看>>
Cap12_项目采购管理
查看>>