1 package cn.liuning.UI;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import cn.liuning.find.FindCETScore;
6 import cn.liuning.find.FindLibrary;
7 import cn.liuning.javabean.Book;
8 import cn.liuning.javabean.Score;
9
10
11 public class Main {
12
13 /**
14 * @param args
15 * @throws Exception
16 */
17 public static void main(String[] args) throws Exception {
18
19 //查询CET
20 //System.out.println("请输入您的准考证号:");
21 //Scanner scanner = new Scanner(System.in);
22 //String examnumber = scanner.next();
23 //System.out.println("请输入您的姓名:");
24 //String name = scanner.next();
25 FindCETScore findCet=new FindCETScore();
26 List<Score> list1 = new ArrayList<Score>();
27 list1 = findCet.findCETScore_Get("", "");
28 findCet.output(list1);
29
30 //查询图书馆借书情况
31 FindLibrary findBook = new FindLibrary();
32 //System.out.println("请输入您的账号:");
33 //Scanner scanner = new Scanner(System.in);
34 //String account = scanner.next();
35 //System.out.println("请输入您的密码:");
36 //String password = scanner.next();
37 List<Book> list2=findBook.findLibrary("","");
38 findBook.output(list2);
39
40 }
41
42 }
43 ------------------------------------------------------------------
44 package cn.liuning.find;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import org.apache.http.NameValuePair;
49 import org.apache.http.client.entity.UrlEncodedFormEntity;
50 import org.apache.http.client.methods.CloseableHttpResponse;
51 import org.apache.http.client.methods.HttpGet;
52 import org.apache.http.client.methods.HttpPost;
53 import org.apache.http.impl.client.CloseableHttpClient;
54 import org.apache.http.impl.client.HttpClients;
55 import org.apache.http.message.BasicNameValuePair;
56 import org.apache.http.util.EntityUtils;
57
58 import cn.liuning.Utils.Book_HtmlToJavaBean;
59 import cn.liuning.Utils.PublicUtils;
60 import cn.liuning.javabean.Book;
61 import cn.liuning.javabean.Score;
62
63
64 public class FindLibrary {
65 CloseableHttpClient httpclient= HttpClients.createDefault();
66 List<Score> list = new ArrayList<Score>();
67
68 //以post方式登陆系统
69 public void findLibrary_Login(String account, String password) throws Exception
70 {
71 HttpPost httpPost = new HttpPost("http://222.206.65.12/reader/redr_verify.php");
72 List <NameValuePair> nvps = new ArrayList <NameValuePair>();
73 nvps.add(new BasicNameValuePair("number", "12110501102"));
74 nvps.add(new BasicNameValuePair("passwd", "12110501102"));
75 nvps.add(new BasicNameValuePair("select", "cert_no"));
76 nvps.add(new BasicNameValuePair("returnUrl", ""));
77 httpPost.setEntity(new UrlEncodedFormEntity(nvps));
78 CloseableHttpResponse response2 = httpclient.execute(httpPost);
79 try {
80 EntityUtils.toString(response2.getEntity());
81 } finally {
82 response2.close();
83 }
84
85 }
86
87 //get方式获取数据
88 public List<Book> findLibrary_Get() throws Exception
89 {
90 HttpGet httpGet = new HttpGet("http://222.206.65.12/reader/book_lst.php");
91 CloseableHttpResponse response1 = httpclient.execute(httpGet);
92 try {
93 String str = EntityUtils.toString(response1.getEntity());
94 str = new String(str.getBytes("iso8859-1"), "utf-8");
95 Book_HtmlToJavaBean book_html=new Book_HtmlToJavaBean();
96 List<Book> list = book_html.changeTobean(str);
97 return list;
98 } finally {
99 response1.close();
100 }
101 }
102
103 //post方式获取
104 public String findLibrary_Post(CloseableHttpClient httpclient) throws Exception
105 {
106 return null;
107 }
108
109 //查询入口
110 public List<Book> findLibrary(String account, String password) throws Exception {
111 findLibrary_Login(account,password);//先登录
112 return findLibrary_Get();//后查询
113 }
114
115 //输出函数
116 public void output(List<Book> list2) {
117
118 for (int j = 0; j < list2.size(); j++) {
119 Book book = list2.get(j);
120 PublicUtils utils = new PublicUtils();
121 String a = utils.bookTostring(book);
122 System.out.println(a);
123
124 }
125 }
126
127 }
128
129 -----------------------------------------------------------
130 package cn.liuning.Utils;
131
132
133 import java.util.ArrayList;
134 import java.util.List;
135
136 import org.jsoup.Jsoup;
137 import org.jsoup.nodes.Document;
138 import org.jsoup.nodes.Element;
139 import org.jsoup.select.Elements;
140
141 import cn.liuning.javabean.Book;
142
143 public class Book_HtmlToJavaBean {
144
145 public List<Book> changeTobean(String str){
146
147 Document doc = Jsoup.parse(str);
148 Element table = doc.getElementsByTag("table").get(0);
149 Elements trs = table.getElementsByTag("tr");
150 ArrayList<Book> list=new ArrayList<Book>();
151
152 for (int i = 1; i < trs.size(); i++)
153 {
154 Element tr1 = trs.get(i);
155 Elements tds = tr1.getElementsByTag("td");
156 Book book = new Book();
157 for (int j = 0; j < tds.size() - 2; j++)
158 {
159 Element td = tds.get(j);
160 String text = td.text();
161
162 if(j==0) {
163 book.setBarcode(text);
164 }
165 else if(j==1) {
166 book.setBookname(text);
167 }
168 else if(j==2) {
169 book.setBookname(text);
170 }
171
172 else if(j==3) {
173 book.setBorrowdate(text);
174 }
175 else if(j==4) {
176 book.setReturndate(text);
177 }
178 else if(j==5) {
179 book.setCollectplace(text);
180 }
181 }
182 list.add(book);
183 }
184
185 return list;
186 }
187 }
188 ----------------------------------------------------------------
189 package cn.liuning.Utils;
190
191 import cn.liuning.javabean.Book;
192
193 public class PublicUtils {
194
195 public String bookTostring(Book book)
196 {
197 return "条码号:" + book.getBarcode()+
198 " "+" 书名:" + book.getBookname()+
199 " "+"责任者:"+ book.getAuthor()+
200 " "+"借阅日期:" + book.getBorrowdate()+
201 " 应还日期:"+book.getReturndate()+
202 " 馆藏地:"+book.getCollectplace();
203 }
204
205 }
206 -----------------------------------------------------------------
207 package cn.liuning.javabean;
208
209 public class Book {
210 /**
211 * 书的条形码
212 */
213 private String barcode;
214 /**
215 * 书名
216 */
217 private String bookname;
218 /**
219 * 责任者
220 */
221 private String author;
222 /**
223 * 借阅日期
224 */
225 private String borrowdate;
226 /**
227 * 应还日期
228 */
229 private String returndate;
230 /**
231 * 馆藏地
232 */
233 private String collectplace;
234
235
236 public String getBarcode() {
237 return barcode;
238 }
239 public void setBarcode(String barcode) {
240 this.barcode = barcode;
241 }
242 public String getBookname() {
243 return bookname;
244 }
245 public void setBookname(String bookname) {
246 this.bookname = bookname;
247 }
248 public String getAuthor() {
249 return author;
250 }
251 public void setAuthor(String author) {
252 this.author = author;
253 }
254 public String getBorrowdate() {
255 return borrowdate;
256 }
257 public void setBorrowdate(String borrowdate) {
258 this.borrowdate = borrowdate;
259 }
260 public String getReturndate() {
261 return returndate;
262 }
263 public void setReturndate(String returndate) {
264 this.returndate = returndate;
265 }
266 public String getCollectplace() {
267 return collectplace;
268 }
269 public void setCollectplace(String collectplace) {
270 this.collectplace = collectplace;
271 }
272 }