標籤:借閱詳情 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);}}}
測試結果:
待續……
擷取個人借閱資訊---圖書館用戶端