这次实现了简单的从网络更新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