在上一篇 获取个人借阅信息---图书馆客户端 已经得到了个人借阅的信息,图书馆对已经借阅的图书还提供了续借的功能。
实现续借功能也不复杂,在上一篇解析个人借阅信息时,添加两个字段即可。即修改getLendBookInfos(String lendInfoHtml)方法。
代码:
/**
* 获取借阅的数目信息
*
* @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_mark = item.getElementsByClass("patFuncMark").get(0);// 单选框
Element ele_input = ele_mark.child(0);
// 续借的实现依赖一下两个字段
String markName = ele_input.attr("id");// eg:renow1
String markValue = ele_input.attr("value");// eg:i4230433
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, markName, markValue);
lendBookInfos.add(bookInfo);
}
return lendBookInfos;
}接着,点击续借,就是又发送了一次post请求,请求体:
此次请求没有真正的执行续借,而是提示你是否确定要续借。
点击是,查看真正的请求,请求体:
响应结果为:
下面进行编码,模拟post请求,只需模拟真正实现续借的那次请求即可(renewsome : 是),代码:
/**
* 续借图书的post请求.(同样必须在login()调用之后)
*
* @param lendBookInfo
* 要续借的图书条目
*
* @return 借阅图书信息的html
*/
public static String renewBook(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");// 续借图书的URL
List<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",
// "续借所选馆藏"));
nameValuePairs.add(new BasicNameValuePair("renewsome", "是"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httpPost);// 发送post请求
int code = response.getStatusLine().getStatusCode();
System.out
.println("---------------renewbook------------------------");
System.out.println(response.getStatusLine());
if (code == 200) {
if (response != null) {
renewBookHtml = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
// System.out.println(renewBookHtml);
return renewBookHtml;
}
}
} catch (Exception e) {
} finally {
httpPost.abort();
}
return "";
}
说明:
续借图书的流程为:
点击某一书目的续借按钮,调用renewBook(LendBookInfo lendBookInfo)方法,得到post请求后的(借阅图书信息的html),对此html进行再次解析,调用List<LendBookInfo>getLendBookInfos(String lendInfoHtml),得到新的图书借阅信息。此时的状态中便发生了相应改变。
测试:
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);
}
//借阅列表的第一本
LendBookInfo lendBookInfo1 = lendBookInfos.get(0);
//续借第一本
String bookinfo = LibraryUtil.renewBook(lendBookInfo1);
// lendInfoHtml = LibraryUtil.getCurLendInfo();
//再次解析html,得到 新的借阅信息列表
lendBookInfos = UserInfoHandler.getLendBookInfos(bookinfo);
for (LendBookInfo bookInfo : lendBookInfos) {
System.out.println(bookInfo);
}
}
}如此便实现了续借功能。。。
原文地址:http://blog.csdn.net/leokelly001/article/details/42028737