擷取個人借閱資訊---圖書館用戶端

來源:互聯網
上載者:User

標籤:借閱詳情   android   jsoup   

在上一篇利用jsoup解析個人資訊----圖書館用戶端,獲得個人基本資料後,便有了進一步的需求:擷取當前借閱的詳細資料

擷取的方法還是一樣的,利用jsoup解析借閱資訊頁面,獲得所需資料,封裝成LendBookInfo,然後將其加入一個List中。

借閱資訊詳情頁如下:

類比get請求得到其html字串,代碼較簡單

/** *擷取當前借閱資訊。 必須在login()調用之後 *  * @return */public static String getCurLendInfo() {String curLendInfo = null;/** *  * location----------- /patroninfo~S3*chx/1****82/top *  * 目標-------------/patroninfo~S3*chx/1****82/items *  * tem_location----/patroninfo~S3*chx/1****82 *  *  */HttpGet httpget = null;String tem_location = location.substring(0, location.lastIndexOf("/"));System.out.println("tem_location---->" + tem_location);try {httpget = new HttpGet(baseUrl + tem_location + "/items");response = httpclient.execute(httpget);// 發送get請求int code = response.getStatusLine().getStatusCode();System.out.println(response.getStatusLine());if (code == 200) {if (response != null) {curLendInfo = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);}}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return curLendInfo;}


得到html後,便可以利用jsoup進行解析。開啟firedebug,進行分析。代碼如下:

/** * 擷取借閱的數目資訊 *  * @param lendInfoHtml *            借閱資訊詳情html * @return 借閱資訊列表 */public static List<LendBookInfo> getLendBookInfos(String lendInfoHtml) {List<LendBookInfo> lendBookInfos = new ArrayList<>();Document document = Jsoup.parse(lendInfoHtml);Element table = document.getElementsByClass("patFunc").get(0);// 表格Elements items = table.getElementsByClass("patFuncEntry");// 數目資訊集合for (Element item : items) {LendBookInfo bookInfo = null;Element ele_title = item.getElementsByClass("patFuncTitle").get(0);// 題名String bookDetail = ele_title.child(0).text();Element ele_barCode = item.getElementsByClass("patFuncBarcode").get(0);// 條碼String barCode = ele_barCode.text();Element ele_status = item.getElementsByClass("patFuncStatus").get(0);// 狀態String status = ele_status.text();Element ele_callNumber = item.getElementsByClass("patFuncCallNo").get(0);// 索書號String callNumber = ele_callNumber.text();bookInfo = new LendBookInfo(bookDetail, callNumber, status, barCode);lendBookInfos.add(bookInfo);}return lendBookInfos;}

測試如下:


public static void main(String[] args) {boolean isConn = LibraryUtil.login(stuNo, password);/** * 若登陸成功則將資訊儲存到資料庫(學號、密碼需要加密)。 */if (isConn) {String resultHtml = LibraryUtil.getResultHtml();UserInfo userInfo = UserInfoHandler.getUserInfo(resultHtml);userInfo.setStuNo(stuNo);userInfo.setPassword(password);System.out.println("========");System.out.println(userInfo.toString());String lendInfoHtml = LibraryUtil.getCurLendInfo();List<LendBookInfo> lendBookInfos = UserInfoHandler.getLendBookInfos(lendInfoHtml);for (LendBookInfo bookInfo : lendBookInfos) {System.out.println(bookInfo);}}}

測試結果:



待續……

擷取個人借閱資訊---圖書館用戶端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.