In the previous article to obtain personal information---library client has been the personal borrowing information, the library has borrowed books also provide the function of renewing.
The implementation of the renewal function is not complicated, in the previous analysis of personal borrowing information, add two fields. That is , modify the Getlendbookinfos (String lendinfohtml) method.
Code:
/** * Get the number of borrowing information * * @param lendinfohtml * Borrowing information details HTML * @return Lending Information list */public static list<lendbookinfo> Getlendbookinfos (String lendinfohtml) {list<lendbookinfo> Lendbookinfos = new arraylist<> ();D ocument Document = Jsoup.parse (lendinfohtml); Element table = Document.getelementsbyclass ("Patfunc"). Get (0);//table Elements items = Table.getelementsbyclass (" Patfuncentry ");//number Information set for (Element item:items) {lendbookinfo bookInfo = null; Element Ele_mark = Item.getelementsbyclass ("Patfuncmark"). Get (0);//Radio box element ele_input = ele_mark.child (0);// The implementation of renewal relies on two fields string markname = Ele_input.attr ("id");//Eg:renow1string Markvalue = ele_input.attr ("value");//Eg: I4230433element ele_title = Item.getelementsbyclass ("Patfunctitle"). Get (0);//title String bookdetail = Ele_title.child (0 ). text (); Element Ele_barcode = Item.getelementsbyclass ("Patfuncbarcode"). Get (0);//Barcode String barCode = Ele_barcode.text (); Element ele_status = Item.getelementsbyclass ("Patfuncstatus"). Get (0);//state string status = Ele_status.text (); Element Ele_callnumber = Item.getelementsbyclass ("Patfunccallno"). Get (0);//Call ISBN string callnumber = Ele_ Callnumber.text (); bookInfo = new Lendbookinfo (Bookdetail, Callnumber, Status,barcode, Markname, Markvalue); Lendbookinfos.add (bookInfo);} return Lendbookinfos;}
Then, click Renew, is sent another post request, the request body:
The request did not really renew, but rather prompted whether you are sure to renew it.
Click Yes to view the real request, the request body:
The response result is:
The following code, which simulates a POST request, simply simulates the request to actually renew the renewal (renewsome: Yes ), and it:
/** * POST request to renew a book. (also must be after login () call) * * @param lendbookinfo * The book entry to be renewed * * @return to borrow the book information HTML */public static String Renewboo K (Lendbookinfo lendbookinfo) {String renewbookhtml = null; String Renew_key = Lendbookinfo.getmarkname (); String Renew_value = Lendbookinfo.getmarkvalue (); HttpPost httppost = null; String tem_location = location.substring (0, Location.lastindexof ("/")); try {httppost = new HttpPost (BASEURL + tem_ Location + "/items");//renewal of books urllist<namevaluepair> namevaluepairs = new arraylist<namevaluepair> (); Namevaluepairs.add (New Basicnamevaluepair ("Currentsortorder", "current_checkout")); Namevaluepairs.add (new Basicnamevaluepair ("Currentsortorder", "current_checkout"); Namevaluepairs.add (New Basicnamevaluepair (Renew_key, Renew_value));//Namevaluepairs.add (New Basicnamevaluepair ("Requestrenewsome",//"Renewing selected Collections"); Namevaluepairs.add ( New Basicnamevaluepair ("Renewsome", "yes"), Httppost.setentity (New Urlencodedformentity (namevaluepairs)); response = Httpclient.exeCute (httppost);//send POST request int code = Response.getstatusline (). Getstatuscode (); System.out.println ("---------------renewbook------------------------"); System.out.println (Response.getstatusline ()), if (code = =) {if (response! = null) {renewbookhtml = Entityutils.tostring (Response.getentity (), HTTP. UTF_8);//System.out.println (renewbookhtml); return renewbookhtml;}}} catch (Exception e) {} finally {Httppost.abort ();} Return "";}
This will be the personal borrowing Information page HTML after the renewal request. You can then parse the HTML to get the status after the renewal, such as:
too fast to renew your visa
Description :
The process for renewing books is:
Click on a book to renew the button, call the Renewbook (Lendbookinfo lendbookinfo) method, after the POST request (borrowing the HTML of the book information), the HTML to re-parse, call list< Lendbookinfo>getlendbookinfos (String lendinfohtml), get new book lending information. A corresponding change has taken place in this state.
Test :
public static void Main (string[] args) {Boolean isconn = Libraryutil.login (Stuno, password);/** * If the login is successful, save the information to the database (number, password required Encryption). */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);} The first lendbookinfo of the borrowing list lendBookInfo1 = lendbookinfos.get (0);//renew the first string bookinfo = Libraryutil.renewbook ( LENDBOOKINFO1);//lendinfohtml = Libraryutil.getcurlendinfo ();//parse HTML again to get a new list of borrowing information Lendbookinfos = Userinfohandler.getlendbookinfos (BookInfo); for (Lendbookinfo Bookinfo:lendbookinfos) {System.out.println (bookinfo );}}}
This will enable the renewal function ...
Renewing the function of books to realize---library client