HttpClient + Jsoup模拟登录教务处并获取课表

栏目:行业动态  时间:2017-03-12
手机版

1 package com.example.upr.net;

2

3 import java.io.BufferedReader;

4 import java.io.IOException;

5 import java.io.InputStream;

6 import java.io.InputStreamReader;

7 import java.io.UnsupportedEncodingException;

8 import java.util.ArrayList;

9 import java.util.List;

10

11 import org.apache.http.HttpEntity;

12 import org.apache.http.HttpResponse;

13 import org.apache.http.NameValuePair;

14 import org.apache.http.client.ClientProtocolException;

15 import org.apache.http.client.HttpClient;

16 import org.apache.http.client.entity.UrlEncodedFormEntity;

17 import org.apache.http.client.methods.HttpGet;

18 import org.apache.http.client.methods.HttpPost;

19 import org.apache.http.cookie.Cookie;

20 import org.apache.http.impl.client.AbstractHttpClient;

21 import org.apache.http.impl.client.DefaultHttpClient;

22 import org.apache.http.message.BasicNameValuePair;

23 import org.jsoup.Jsoup;

24 import org.jsoup.nodes.Document;

25 import org.jsoup.nodes.Element;

26 import org.jsoup.select.Elements;

27

28 import com.example.upr.CompleteListener;

29

30 import android.R.integer;

31 import android.os.AsyncTask;

32 import android.provider.ContactsContract.Contacts.Data;

33 import android.util.Log;

34 import android.widget.Toast;

35

36 public class Login extends AsyncTask{

37

38 private String zjh;

39 private String mm;

40 private String webPage;

41 private String [][] kebiao=new String[15][8];

42 private CompleteListener listener;

43 public Login(String account, String password, CompleteListener listener) {

44 zjh= account;

45 mm= password;

46

47 this.listener= listener;

48 }

49 @Override

50 protected String[][] doInBackground(Void... arg0) {

51 List list=new ArrayList();

52 list.add(new BasicNameValuePair("zjh", zjh));

53 list.add(new BasicNameValuePair("mm", mm));//表单信息

54 String encode="gb2312";//编码格式,从F12工具中可以找到

55 HttpClient httpClient=new DefaultHttpClient();

56 List cookies;

57 try {

58 HttpEntity entity=new UrlEncodedFormEntity(list ,encode);//封装数据

59 HttpPost post=new HttpPost(com.example.upr.Constant.LOGIN_URL);//建立POST请求

60 post.setEntity(entity);

61 HttpResponse httpResponse= httpClient.execute(post);

62 String data, result;

63 if (httpResponse.getStatusLine().getStatusCode()==200) {//登录成功

64 cookies=((AbstractHttpClient) httpClient).getCookieStore().getCookies();//获取Cookie

65 HttpGet httpGet=new HttpGet("");

66 httpGet.setHeader("Cookie", "JSESSIONID="+ cookies.get(0).getValue()+";"+

67 "NSC_kjbpxv-iuuq="+cookies.get(1).getValue());//注意分号

68 httpResponse=new DefaultHttpClient().execute(httpGet);

69

70 StringBuffer sb=new StringBuffer();

71 entity= httpResponse.getEntity();

72 InputStream inputStream= entity.getContent();

73 BufferedReader br=new BufferedReader(new InputStreamReader(inputStream, encode));

74 while ((data=br.readLine()) !=null) {

75 sb.append(data); //读取返回的网页

76 }

77 result= sb.toString();

78 webPage= result;

79

80 } else {

81 return null;

82 }

83 } catch (UnsupportedEncodingException e) {

84 e.printStackTrace();

85 return null;

86 } catch (ClientProtocolException e) {

87 // TODO Auto-generated catch block

88 e.printStackTrace();

89 return null;

90 } catch (IOException e) {

91 // TODO Auto-generated catch block

92 e.printStackTrace();

93 return null;

94 }

95

96 Document document= Jsoup.parse(webPage);

97 if (document !=null) {

98 Element element=document.getElementById("user");//通过id直接定位到数据

99 Elements trElements=element.select("tr");

100 //以下是提取课表信息,每个网站不同,一下代码要解析的html放在最下面了

101 for (int i=0; i < trElements.size(); i++) {

102 Elements tdElements=trElements.get(i).select("td");

103 for (int j=0; j < tdElements.size(); j++) {

104 String text= tdElements.get(j).text();

105 if (tdElements.size()==1) {

106 continue;

107 } else if (tdElements.size()==9) {

108 if (j !=0) {

109 kebiao[i][j-1]= text;

110 }

111 }else {

112 kebiao[i][j]= text;

113 }

114

115 }

116 }

117 return kebiao;

118 } else {

119 return null;

120 }

121

122 }

123 @Override

124 protected void onPostExecute(String[][] result) {

125 super.onPostExecute(result);

126 listener.onConnectFinish(result);//执行回调函数,更新主UI

127 }

128 }

129

上一篇:小升初名校见面会 更看重“潜力”而非成绩
下一篇:如果霍建华是你的教务处主任

最近更新行业动态