AndroidWidget实践 --- EverydayTips开发(6)
生活随笔
收集整理的這篇文章主要介紹了
AndroidWidget实践 --- EverydayTips开发(6)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
這次實(shí)現(xiàn)了簡(jiǎn)單的從網(wǎng)絡(luò)更新tips.txt
先檢查是否有更新,使用的sae的空間
網(wǎng)址是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 responseHttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 若狀態(tài)碼為200 okif (httpResponse.getStatusLine().getStatusCode() != 404) {// 取出回應(yīng)字串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();// 保存現(xiàn)在的更新日期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數(shù)據(jù)byte[] data = StreamTool.readInputStream(inStream);// 得到html的二進(jìn)制數(shù)據(jù)DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(DATA_PATH + "tips.txt"));dataOutputStream.write(data);dataOutputStream.flush();return true;}http://dl.dbank.com/c0ez81qgts
轉(zhuǎn)載于:https://my.oschina.net/sfshine/blog/470696
總結(jié)
以上是生活随笔為你收集整理的AndroidWidget实践 --- EverydayTips开发(6)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第13/24周 统计信息
- 下一篇: 关于Android中Animation的