diff --git a/README.md b/README.md index 3893c53..daa452d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ NJUNet Android App ====================== -NJUNet是一个用于安卓手机连接到NJU-WLAN后自动登陆p.nju.edu.cn的Android程序,代替手动打开浏览器登陆的繁琐操作,做到有NJU-WLAN的地方总是保持网络连接。 +NJUNet是一个用于安卓手机连接到南大校园网(包括宿舍WiFi)后自动登陆p.nju.edu.cn的Android程序,代替手动打开浏览器登陆的繁琐操作,做到有NJU-WLAN的地方总是保持网络连接。

适用平台

Android 4.0+ @@ -22,7 +22,7 @@ Android 4.0+

功能说明

- 可手动进行登陆,下线,查看登陆数据(时长,余额等) -- 默认开启后台服务,将在连接上南大校园网WLAN时自动登陆 +- 默认开启后台服务,将在连接上南大校园网WLAN(包括宿舍路由器无线网)时自动登陆 - 可以使用状态栏通知快捷登陆

下载

@@ -30,7 +30,7 @@ Android 4.0+ 或 -[apk download](https://raw.githubusercontent.com/padeoe/AutoConnect/newUI/app/app-release.apk) +[apk-v2.5.1](https://raw.githubusercontent.com/padeoe/AutoConnect/newUI/app/app-release.apk)

更新日志

diff --git a/UPDATE.md b/UPDATE.md index 92a5290..2286978 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -1,5 +1,12 @@ 更新日志 -======= +==== +

v2.5.1(2016年6月4日)

+修复了一些小问题 + +- 修改账号信息后数据不能立即被更新 +- 退出程序时可能会产生一次后台登陆 +- 在主界面显示用户信息 +

v2.5(2016年6月1日)

- 更新了所有界面及风格 - 可以显示详单 diff --git a/app/app-release.apk b/app/app-release.apk index 8e24803..3b19b9e 100644 Binary files a/app/app-release.apk and b/app/app-release.apk differ diff --git a/app/build.gradle b/app/build.gradle index 1ea1aae..fbdf549 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.padeoe.njunet" minSdkVersion 14 targetSdkVersion 23 - versionCode 1 - versionName "2.5" + versionCode 2 + versionName "2.5.1" } buildTypes { release { diff --git a/app/src/main/java/com/padeoe/bookplus/BookPlus.java b/app/src/main/java/com/padeoe/bookplus/BookPlus.java index dd433e1..11bb06b 100644 --- a/app/src/main/java/com/padeoe/bookplus/BookPlus.java +++ b/app/src/main/java/com/padeoe/bookplus/BookPlus.java @@ -3,6 +3,7 @@ import com.padeoe.icroom.util.NetworkUtils; import com.padeoe.utils.LoginException; +import java.io.IOException; import java.util.HashMap; /** @@ -21,7 +22,7 @@ public class BookPlus { * @param password 南京大学图书馆Book+的密码:初始密码是用户名 * @throws LoginException 登陆失败 */ - public BookPlus(String username, String password) throws LoginException { + public BookPlus(String username, String password) throws LoginException, IOException { String[] returnInfo = login(username, password); cookie = returnInfo[1]; } @@ -43,7 +44,7 @@ public BookPlus(String cookie) { * @return 一个长度为2的字符数组,第一个元素是服务器返回的数据,第二个是获取的cookie * @throws LoginException 登陆失败 */ - public static String[] login(String username, String password) throws LoginException { + public static String[] login(String username, String password) throws LoginException, IOException { double random = Math.random(); String url = "http://book.njulib.cn/smarty_lib/control/controller.php?control=control_log&action=user_login&user_id=" + username + "&user_pwd=" + password + "&rand=" + random; String returnInfo[] = NetworkUtils.myPostAndGetCookie("control=control_log&action=user_login&user_id=" + username + "&user_pwd=" + password + "&rand=" + random, url, 2000); @@ -61,7 +62,7 @@ public static String[] login(String username, String password) throws LoginExcep * * @return 服务器返回的包括姓名和性别的html文本 */ - public String showReaderInfo() { + public String showReaderInfo() throws IOException { HashMap attr = new HashMap<>(); attr.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); attr.put("Accept-Encoding", "gzip, deflate, sdch"); @@ -78,8 +79,14 @@ public String showReaderInfo() { * @return 用户的性别,“男”或“女” */ public String showGender() { - String returnData = showReaderInfo(); - return getGender(returnData); + String returnData = null; + try { + returnData = showReaderInfo(); + return getGender(returnData); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } } /** diff --git a/app/src/main/java/com/padeoe/icroom/ICRoom.java b/app/src/main/java/com/padeoe/icroom/ICRoom.java index e6d21f5..38150e9 100644 --- a/app/src/main/java/com/padeoe/icroom/ICRoom.java +++ b/app/src/main/java/com/padeoe/icroom/ICRoom.java @@ -3,6 +3,8 @@ import com.padeoe.icroom.util.NetworkUtils; import com.padeoe.utils.LoginException; +import java.io.IOException; + /** * 该类用于实现 南京大学IC空间管理系统 的接口 * @author padeoe @@ -18,7 +20,7 @@ public class ICRoom { * @param username 用户名:学号/工号 * @param password 南京大学IC空间管理系统的密码:初始值是用户名 */ - public ICRoom(String username, String password) throws LoginException { + public ICRoom(String username, String password) throws LoginException, IOException { String[] returnData = login(username, password); cookie = returnData[1]; } @@ -43,7 +45,7 @@ public ICRoom(String cookie) { * @return 数组长度为2,第一个元素是服务器返回的原始数据,第二个元素是cookie * @throws LoginException 登陆失败 */ - public static String[] login(String username, String password) throws LoginException { + public static String[] login(String username, String password) throws LoginException, IOException { String returnInfo[] = NetworkUtils.myPostAndGetCookie("id=" + username + "&pwd=" + password + "&act=login", "http://114.212.7.24/ClientWeb/pro/ajax/login.aspx", 10000); if (returnInfo == null || returnInfo[0] == null || returnInfo[0].startsWith("{\"ret\":0")) { if (returnInfo != null &&returnInfo[0] != null) @@ -60,7 +62,7 @@ public static String[] login(String username, String password) throws LoginExcep * @param ID 用户名:学号/工号 * @return 服务器返回的用户信息字符串,非json格式,需要调用 {@link #parse(String)}方法处理成json格式 */ - public String queryStudentInfo(String ID) { + public String queryStudentInfo(String ID) throws IOException { String time = String.valueOf(System.currentTimeMillis()); return NetworkUtils.mygetWithCookie("type=&&term=" + ID + "&_=" + time, "http://114.212.7.24/ClientWeb/pro/ajax/data/searchAccount.aspx?type=&&term=" + ID + "&_=" + time, cookie, 20000); @@ -95,7 +97,7 @@ public static String parse(String result){ * @param ID 用户名:学号/工号 * @return 用户信息的对象格式 {@link ICRoomUser} */ - public ICRoomUser queryStudentInfo_Object(String ID){ + public ICRoomUser queryStudentInfo_Object(String ID) throws IOException { String result = queryStudentInfo(ID); String newResult=parse(result); return ICRoomUser.getFromJson(newResult); diff --git a/app/src/main/java/com/padeoe/icroom/util/NetworkUtils.java b/app/src/main/java/com/padeoe/icroom/util/NetworkUtils.java index 01c3a32..fae3750 100644 --- a/app/src/main/java/com/padeoe/icroom/util/NetworkUtils.java +++ b/app/src/main/java/com/padeoe/icroom/util/NetworkUtils.java @@ -2,6 +2,7 @@ import com.padeoe.utils.network.MyHttpRequest; +import java.io.IOException; import java.util.HashMap; /** @@ -10,7 +11,7 @@ * Date: 2016/3/7 */ public class NetworkUtils { - public static String mygetWithCookie(String data,String URL,String cookie,int timeout){ + public static String mygetWithCookie(String data,String URL,String cookie,int timeout) throws IOException { HashMap attr=new HashMap<>(); attr.put("Accept","application/json, text/javascript, */*; q=0.01"); attr.put("Referer", "http://114.212.7.24/ClientWeb/xcus/IC2/Default.aspx"); @@ -18,7 +19,7 @@ public static String mygetWithCookie(String data,String URL,String cookie,int ti attr.put("X-Requested-With", "XMLHttpRequest"); return MyHttpRequest.getWithCookie(data,URL,attr,cookie,"UTF-8", "UTF-8",timeout); } - public static String[] myPostAndGetCookie(String data,String URL,int timeout){ + public static String[] myPostAndGetCookie(String data,String URL,int timeout) throws IOException { HashMap attr=new HashMap<>(); attr.put("Accept","application/json, text/javascript, */*; q=0.01"); attr.put("Referer", "http://114.212.7.24/ClientWeb/xcus/IC2/Default.aspx"); diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Acct.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Acct.java deleted file mode 100644 index 33a1569..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Acct.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.annotation.JSONField; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 详单信息 - */ -public class Acct extends Base { - @JSONField(name = "rows") - private AcctRow[] acctRows; - - public Acct() { - } - - public static Acct getFromJson(String jsonobject) { - try { - Acct acct = JSON.parseObject(jsonobject, Acct.class); - return acct; - } catch (Exception e) { - return null; - } - } - - public AcctRow[] getAcctRows() { - return acctRows; - } - - public void setAcctRows(AcctRow[] acctRows) { - this.acctRows = acctRows; - } - - @Override - public String toString() { - return "ClassPojo [total = " + total + ", reply_code = " + reply_code + ", rows = " + acctRows + ", reply_msg = " + reply_msg + "]"; - } -} - diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AcctRow.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AcctRow.java deleted file mode 100644 index ef4b17c..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AcctRow.java +++ /dev/null @@ -1,328 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -import com.alibaba.fastjson.annotation.JSONField; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 详单信息的行 - */ -public class AcctRow { - @JSONField(name = "pvlan") - private String pvlan; - @JSONField(name = "service_id") - private String service_id; - @JSONField(name = "area_id") - private String area_id; - @JSONField(name = "acctsessiontime") - private String acctsessiontime; - @JSONField(name = "mac") - private String mac; - @JSONField(name = "service_name") - private String service_name; - @JSONField(name = "ap_id") - private String ap_id; - @JSONField(name = "area_type") - private String area_type; - @JSONField(name = "id") - private String id; - @JSONField(name = "username") - private String username; - @JSONField(name = "acctstarttime") - private String acctstarttime; - @JSONField(name = "area_name") - private String area_name; - @JSONField(name = "user_id") - private String user_id; - @JSONField(name = "acctoutputoctets_ipv4") - private String acctoutputoctets_ipv4; - @JSONField(name = "user_ipv4") - private String user_ipv4; - @JSONField(name = "refer_ipv6") - private String refer_ipv6; - @JSONField(name = "fullname") - private String fullname; - @JSONField(name = "acctoutputoctets_ipv6") - private String acctoutputoctets_ipv6; - @JSONField(name = "user_ipv6") - private String user_ipv6; - @JSONField(name = "refer_ipv4") - private String refer_ipv4; - @JSONField(name = "acctinputoctets_ipv6") - private String acctinputoctets_ipv6; - @JSONField(name = "acctinputoctets_ipv4") - private String acctinputoctets_ipv4; - @JSONField(name = "subport") - private String subport; - @JSONField(name = "acctsessionid") - private String acctsessionid; - @JSONField(name = "amount_ipv6") - private String amount_ipv6; - @JSONField(name = "acctstoptime") - private String acctstoptime; - @JSONField(name = "amount_ipv4") - private String amount_ipv4; - @JSONField(name = "svlan") - private String svlan; - @JSONField(name = "acctterminatecause") - private String acctterminatecause; - @JSONField(name = "src_ip") - private String src_ip; - @JSONField(name = "nas_ip") - private String nas_ip; - - public String getPvlan() { - return pvlan; - } - - public void setPvlan(String pvlan) { - this.pvlan = pvlan; - } - - public String getService_id() { - return service_id; - } - - public void setService_id(String service_id) { - this.service_id = service_id; - } - - public String getArea_id() { - return area_id; - } - - public void setArea_id(String area_id) { - this.area_id = area_id; - } - - public String getAcctsessiontime() { - return acctsessiontime; - } - - public void setAcctsessiontime(String acctsessiontime) { - this.acctsessiontime = acctsessiontime; - } - - public String getMac() { - return mac; - } - - public void setMac(String mac) { - this.mac = mac; - } - - public String getService_name() { - return service_name; - } - - public void setService_name(String service_name) { - this.service_name = service_name; - } - - public String getAp_id() { - return ap_id; - } - - public void setAp_id(String ap_id) { - this.ap_id = ap_id; - } - - public String getArea_type() { - return area_type; - } - - public void setArea_type(String area_type) { - this.area_type = area_type; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getAcctstarttime() { - return acctstarttime; - } - - public void setAcctstarttime(String acctstarttime) { - this.acctstarttime = acctstarttime; - } - - public String getArea_name() { - return area_name; - } - - public void setArea_name(String area_name) { - this.area_name = area_name; - } - - public String getUser_id() { - return user_id; - } - - public void setUser_id(String user_id) { - this.user_id = user_id; - } - - public String getAcctoutputoctets_ipv4() { - return acctoutputoctets_ipv4; - } - - public void setAcctoutputoctets_ipv4(String acctoutputoctets_ipv4) { - this.acctoutputoctets_ipv4 = acctoutputoctets_ipv4; - } - - public String getUser_ipv4() { - return user_ipv4; - } - - public void setUser_ipv4(String user_ipv4) { - this.user_ipv4 = user_ipv4; - } - - public String getRefer_ipv6() { - return refer_ipv6; - } - - public void setRefer_ipv6(String refer_ipv6) { - this.refer_ipv6 = refer_ipv6; - } - - public String getFullname() { - return fullname; - } - - public void setFullname(String fullname) { - this.fullname = fullname; - } - - public String getAcctoutputoctets_ipv6() { - return acctoutputoctets_ipv6; - } - - public void setAcctoutputoctets_ipv6(String acctoutputoctets_ipv6) { - this.acctoutputoctets_ipv6 = acctoutputoctets_ipv6; - } - - public String getUser_ipv6() { - return user_ipv6; - } - - public void setUser_ipv6(String user_ipv6) { - this.user_ipv6 = user_ipv6; - } - - public String getRefer_ipv4() { - return refer_ipv4; - } - - public void setRefer_ipv4(String refer_ipv4) { - this.refer_ipv4 = refer_ipv4; - } - - public String getAcctinputoctets_ipv6() { - return acctinputoctets_ipv6; - } - - public void setAcctinputoctets_ipv6(String acctinputoctets_ipv6) { - this.acctinputoctets_ipv6 = acctinputoctets_ipv6; - } - - public String getAcctinputoctets_ipv4() { - return acctinputoctets_ipv4; - } - - public void setAcctinputoctets_ipv4(String acctinputoctets_ipv4) { - this.acctinputoctets_ipv4 = acctinputoctets_ipv4; - } - - public String getSubport() { - return subport; - } - - public void setSubport(String subport) { - this.subport = subport; - } - - public String getAcctsessionid() { - return acctsessionid; - } - - public void setAcctsessionid(String acctsessionid) { - this.acctsessionid = acctsessionid; - } - - public String getAmount_ipv6() { - return amount_ipv6; - } - - public void setAmount_ipv6(String amount_ipv6) { - this.amount_ipv6 = amount_ipv6; - } - - public String getAcctstoptime() { - return acctstoptime; - } - - public void setAcctstoptime(String acctstoptime) { - this.acctstoptime = acctstoptime; - } - - public String getAmount_ipv4() { - return amount_ipv4; - } - - public void setAmount_ipv4(String amount_ipv4) { - this.amount_ipv4 = amount_ipv4; - } - - public String getSvlan() { - return svlan; - } - - public void setSvlan(String svlan) { - this.svlan = svlan; - } - - public String getAcctterminatecause() { - return acctterminatecause; - } - - public void setAcctterminatecause(String acctterminatecause) { - this.acctterminatecause = acctterminatecause; - } - - public String getSrc_ip() { - return src_ip; - } - - public void setSrc_ip(String src_ip) { - this.src_ip = src_ip; - } - - public String getNas_ip() { - return nas_ip; - } - - public void setNas_ip(String nas_ip) { - this.nas_ip = nas_ip; - } - - @Override - public String toString() { - return "ClassPojo [pvlan = " + pvlan + ", service_id = " + service_id + ", area_id = " + area_id + ", acctsessiontime = " + acctsessiontime + ", mac = " + mac + ", service_name = " + service_name + ", ap_id = " + ap_id + ", area_type = " + area_type + ", id = " + id + ", username = " + username + ", acctstarttime = " + acctstarttime + ", area_name = " + area_name + ", user_id = " + user_id + ", acctoutputoctets_ipv4 = " + acctoutputoctets_ipv4 + ", user_ipv4 = " + user_ipv4 + ", refer_ipv6 = " + refer_ipv6 + ", fullname = " + fullname + ", acctoutputoctets_ipv6 = " + acctoutputoctets_ipv6 + ", user_ipv6 = " + user_ipv6 + ", refer_ipv4 = " + refer_ipv4 + ", acctinputoctets_ipv6 = " + acctinputoctets_ipv6 + ", acctinputoctets_ipv4 = " + acctinputoctets_ipv4 + ", subport = " + subport + ", acctsessionid = " + acctsessionid + ", amount_ipv6 = " + amount_ipv6 + ", acctstoptime = " + acctstoptime + ", amount_ipv4 = " + amount_ipv4 + ", svlan = " + svlan + ", acctterminatecause = " + acctterminatecause + ", src_ip = " + src_ip + ", nas_ip = " + nas_ip + "]"; - } -} diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AuthLog.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AuthLog.java deleted file mode 100644 index 105c4bb..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AuthLog.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.annotation.JSONField; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 认证信息 - */ -public class AuthLog extends Base { - @JSONField(name = "rows") - private AuthLogRow[] authLogRows; - - public AuthLog() { - } - - public static AuthLog getFromJson(String jsonobject) { - try { - AuthLog authLog = JSON.parseObject(jsonobject, AuthLog.class); - return authLog; - } catch (Exception e) { - return null; - } - } - - public AuthLogRow[] getAuthLogRows() { - return authLogRows; - } - - public void setAuthLogRows(AuthLogRow[] authLogRows) { - this.authLogRows = authLogRows; - } - - @Override - public String toString() { - return "ClassPojo [total = " + total + ", reply_code = " + reply_code + ", rows = " + authLogRows + ", reply_msg = " + reply_msg + "]"; - } -} - diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AuthLogRow.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AuthLogRow.java deleted file mode 100644 index 834d7e8..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/AuthLogRow.java +++ /dev/null @@ -1,193 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -import com.alibaba.fastjson.annotation.JSONField; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 登陆日志的行 - */ -public class AuthLogRow { - @JSONField(name = "pvlan") - private String pvlan; - @JSONField(name = "area_id") - private String area_id; - @JSONField(name = "subport") - private String subport; - @JSONField(name = "reply_code") - private String reply_code; - @JSONField(name = "acctsessionid") - private String acctsessionid; - @JSONField(name = "mac") - private String mac; - @JSONField(name = "ap_id") - private String ap_id; - @JSONField(name = "reply_msg") - private String reply_msg; - @JSONField(name = "id") - private String id; - @JSONField(name = "area_type") - private String area_type; - @JSONField(name = "svlan") - private String svlan; - @JSONField(name = "username") - private String username; - @JSONField(name = "area_name") - private String area_name; - @JSONField(name = "src_ip") - private String src_ip; - @JSONField(name = "nas_ip") - private String nas_ip; - @JSONField(name = "datetime") - private String datetime; - @JSONField(name = "area_type_name") - private String area_type_name; - - public AuthLogRow() { - } - - public String getPvlan() { - return pvlan; - } - - public void setPvlan(String pvlan) { - this.pvlan = pvlan; - } - - public String getArea_id() { - return area_id; - } - - public void setArea_id(String area_id) { - this.area_id = area_id; - } - - public String getSubport() { - return subport; - } - - public void setSubport(String subport) { - this.subport = subport; - } - - public String getReply_code() { - return reply_code; - } - - public void setReply_code(String reply_code) { - this.reply_code = reply_code; - } - - public String getAcctsessionid() { - return acctsessionid; - } - - public void setAcctsessionid(String acctsessionid) { - this.acctsessionid = acctsessionid; - } - - public String getMac() { - return mac; - } - - public void setMac(String mac) { - this.mac = mac; - } - - public String getAp_id() { - return ap_id; - } - - public void setAp_id(String ap_id) { - this.ap_id = ap_id; - } - - public String getReply_msg() { - return reply_msg; - } - - public void setReply_msg(String reply_msg) { - this.reply_msg = reply_msg; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getArea_type() { - return area_type; - } - - public void setArea_type(String area_type) { - this.area_type = area_type; - } - - public String getSvlan() { - return svlan; - } - - public void setSvlan(String svlan) { - this.svlan = svlan; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getArea_name() { - return area_name; - } - - public void setArea_name(String area_name) { - this.area_name = area_name; - } - - public String getSrc_ip() { - return src_ip; - } - - public void setSrc_ip(String src_ip) { - this.src_ip = src_ip; - } - - public String getNas_ip() { - return nas_ip; - } - - public void setNas_ip(String nas_ip) { - this.nas_ip = nas_ip; - } - - public String getDatetime() { - return datetime; - } - - public void setDatetime(String datetime) { - this.datetime = datetime; - } - - public String getArea_type_name() { - return area_type_name; - } - - public void setArea_type_name(String area_type_name) { - this.area_type_name = area_type_name; - } - - @Override - public String toString() { - return "ClassPojo [pvlan = " + pvlan + ", area_id = " + area_id + ", subport = " + subport + ", reply_code = " + reply_code + ", acctsessionid = " + acctsessionid + ", mac = " + mac + ", ap_id = " + ap_id + ", reply_msg = " + reply_msg + ", id = " + id + ", area_type = " + area_type + ", svlan = " + svlan + ", username = " + username + ", area_name = " + area_name + ", src_ip = " + src_ip + ", nas_ip = " + nas_ip + ", datetime = " + datetime + ", area_type_name = " + area_type_name + "]"; - } -} - - diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Base.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Base.java deleted file mode 100644 index b51a75c..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Base.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 基类 - */ -public class Base { - protected String total; - - protected String reply_code; - - protected String reply_msg; - - public String getTotal() { - return total; - } - - public void setTotal(String total) { - this.total = total; - } - - public String getReply_code() { - return reply_code; - } - - public void setReply_code(String reply_code) { - this.reply_code = reply_code; - } - - public String getReply_msg() { - return reply_msg; - } - - public void setReply_msg(String reply_msg) { - this.reply_msg = reply_msg; - } -} diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BasicInfo.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BasicInfo.java deleted file mode 100644 index 28358d7..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BasicInfo.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -/** - * Created by padeoe on 2015/9/23. - */ - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.annotation.JSONField; - -/** - * 当前登陆信息 - */ -public class BasicInfo extends Base { - @JSONField(name = "rows") - private BasicInfoRow[] basicInfoRows; - - public static BasicInfo getFromJson(String jsonobject) { - try { - BasicInfo basicInfo = JSON.parseObject(jsonobject, BasicInfo.class); - return basicInfo; - } catch (Exception e) { - return null; - } - } - - public BasicInfoRow[] getBasicInfoRows() { - return basicInfoRows; - } - - public void setBasicInfoRows(BasicInfoRow[] basicInfoRows) { - this.basicInfoRows = basicInfoRows; - } - - - @Override - public String toString() { - return "ClassPojo [total = " + total + ", reply_code = " + reply_code + ", rows = " + basicInfoRows + ", reply_msg = " + reply_msg + "]"; - } -} \ No newline at end of file diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BasicInfoRow.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BasicInfoRow.java deleted file mode 100644 index 68e5969..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BasicInfoRow.java +++ /dev/null @@ -1,185 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 登录信息的行 - */ -public class BasicInfoRow { - private String total_time; - - private String service_id; - - private String total_refer_ipv6; - - private String ipv6_units; - - private String total_ipv6_volume; - - private String ipv4_units; - - private String total_input_octets_ipv6; - - private String service_name; - - private String total_input_octets_ipv4; - - private String id; - - private String username; - - private String month; - - private String total_output_octets_ipv4; - - private String user_id; - - private String total_output_octets_ipv6; - - private String total_refer_ipv4; - - private String total_ipv4_volume; - - public String getTotal_time() { - return total_time; - } - - public void setTotal_time(String total_time) { - this.total_time = total_time; - } - - public String getService_id() { - return service_id; - } - - public void setService_id(String service_id) { - this.service_id = service_id; - } - - public String getTotal_refer_ipv6() { - return total_refer_ipv6; - } - - public void setTotal_refer_ipv6(String total_refer_ipv6) { - this.total_refer_ipv6 = total_refer_ipv6; - } - - public String getIpv6_units() { - return ipv6_units; - } - - public void setIpv6_units(String ipv6_units) { - this.ipv6_units = ipv6_units; - } - - public String getTotal_ipv6_volume() { - return total_ipv6_volume; - } - - public void setTotal_ipv6_volume(String total_ipv6_volume) { - this.total_ipv6_volume = total_ipv6_volume; - } - - public String getIpv4_units() { - return ipv4_units; - } - - public void setIpv4_units(String ipv4_units) { - this.ipv4_units = ipv4_units; - } - - public String getTotal_input_octets_ipv6() { - return total_input_octets_ipv6; - } - - public void setTotal_input_octets_ipv6(String total_input_octets_ipv6) { - this.total_input_octets_ipv6 = total_input_octets_ipv6; - } - - public String getService_name() { - return service_name; - } - - public void setService_name(String service_name) { - this.service_name = service_name; - } - - public String getTotal_input_octets_ipv4() { - return total_input_octets_ipv4; - } - - public void setTotal_input_octets_ipv4(String total_input_octets_ipv4) { - this.total_input_octets_ipv4 = total_input_octets_ipv4; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getMonth() { - return month; - } - - public void setMonth(String month) { - this.month = month; - } - - public String getTotal_output_octets_ipv4() { - return total_output_octets_ipv4; - } - - public void setTotal_output_octets_ipv4(String total_output_octets_ipv4) { - this.total_output_octets_ipv4 = total_output_octets_ipv4; - } - - public String getUser_id() { - return user_id; - } - - public void setUser_id(String user_id) { - this.user_id = user_id; - } - - public String getTotal_output_octets_ipv6() { - return total_output_octets_ipv6; - } - - public void setTotal_output_octets_ipv6(String total_output_octets_ipv6) { - this.total_output_octets_ipv6 = total_output_octets_ipv6; - } - - public String getTotal_refer_ipv4() { - return total_refer_ipv4; - } - - public void setTotal_refer_ipv4(String total_refer_ipv4) { - this.total_refer_ipv4 = total_refer_ipv4; - } - - public String getTotal_ipv4_volume() { - return total_ipv4_volume; - } - - public void setTotal_ipv4_volume(String total_ipv4_volume) { - this.total_ipv4_volume = total_ipv4_volume; - } - - @Override - public String toString() { - return "ClassPojo [total_time = " + total_time + ", service_id = " + service_id + ", total_refer_ipv6 = " + total_refer_ipv6 + ", ipv6_units = " + ipv6_units + ", total_ipv6_volume = " + total_ipv6_volume + ", ipv4_units = " + ipv4_units + ", total_input_octets_ipv6 = " + total_input_octets_ipv6 + ", service_name = " + service_name + ", total_input_octets_ipv4 = " + total_input_octets_ipv4 + ", id = " + id + ", username = " + username + ", month = " + month + ", total_output_octets_ipv4 = " + total_output_octets_ipv4 + ", user_id = " + user_id + ", total_output_octets_ipv6 = " + total_output_octets_ipv6 + ", total_refer_ipv4 = " + total_refer_ipv4 + ", total_ipv4_volume = " + total_ipv4_volume + "]"; - } -} diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Bills.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Bills.java deleted file mode 100644 index 8e48398..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Bills.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.annotation.JSONField; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 账单信息 - */ -public class Bills extends Base { - @JSONField(name = "rows") - private BillsRow[] billsRows; - - public Bills() { - } - - public static Bills getFromJson(String jsonobject) { - try { - Bills bills = JSON.parseObject(jsonobject, Bills.class); - return bills; - } catch (Exception e) { - return null; - } - } - - public BillsRow[] getBillsRows() { - return billsRows; - } - - public void setBillsRows(BillsRow[] billsRows) { - this.billsRows = billsRows; - } - - @Override - public String toString() { - return "ClassPojo [total = " + total + ", reply_code = " + reply_code + ", rows = " + billsRows + ", reply_msg = " + reply_msg + "]"; - } -} - - diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BillsRow.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BillsRow.java deleted file mode 100644 index 72c88b2..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/BillsRow.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 账单信息的行 - */ -public class BillsRow { - private String createtime; - - private String id; - - private String beginning_balance; - - private String startdate; - - private String recharge_amount; - - private String ending_balance; - - private String enddate; - - private String costs_amount; - - private String account_no; - - public String getCreatetime() { - return createtime; - } - - public void setCreatetime(String createtime) { - this.createtime = createtime; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getBeginning_balance() { - return beginning_balance; - } - - public void setBeginning_balance(String beginning_balance) { - this.beginning_balance = beginning_balance; - } - - public String getStartdate() { - return startdate; - } - - public void setStartdate(String startdate) { - this.startdate = startdate; - } - - public String getRecharge_amount() { - return recharge_amount; - } - - public void setRecharge_amount(String recharge_amount) { - this.recharge_amount = recharge_amount; - } - - public String getEnding_balance() { - return ending_balance; - } - - public void setEnding_balance(String ending_balance) { - this.ending_balance = ending_balance; - } - - public String getEnddate() { - return enddate; - } - - public void setEnddate(String enddate) { - this.enddate = enddate; - } - - public String getCosts_amount() { - return costs_amount; - } - - public void setCosts_amount(String costs_amount) { - this.costs_amount = costs_amount; - } - - public String getAccount_no() { - return account_no; - } - - public void setAccount_no(String account_no) { - this.account_no = account_no; - } - - @Override - public String toString() { - return "ClassPojo [createtime = " + createtime + ", id = " + id + ", beginning_balance = " + beginning_balance + ", startdate = " + startdate + ", recharge_amount = " + recharge_amount + ", ending_balance = " + ending_balance + ", enddate = " + enddate + ", costs_amount = " + costs_amount + ", account_no = " + account_no + "]"; - } -} diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Recharge.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Recharge.java deleted file mode 100644 index 0ec6628..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/Recharge.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -import com.alibaba.fastjson.annotation.JSONField; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 充值信息 - */ -public class Recharge extends Base { - @JSONField(name = "rows") - private RechargeRow[] rechargeRows; - - public RechargeRow[] getRechargeRows() { - return rechargeRows; - } - - public void setRechargeRows(RechargeRow[] rechargeRows) { - this.rechargeRows = rechargeRows; - } - - @Override - public String toString() { - return "ClassPojo [total = " + total + ", reply_code = " + reply_code + ", rows = " + rechargeRows + ", reply_msg = " + reply_msg + "]"; - } -} diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/RechargeRow.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/object/RechargeRow.java deleted file mode 100644 index 28c38f5..0000000 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/object/RechargeRow.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.padeoe.nicservice.njuwlan.object; - -/** - * Created by padeoe on 2015/9/23. - */ - -/** - * 充值信息的行 - */ -public class RechargeRow { - private String id; - - private String amount; - - private String remark; - - private String oper_time; - - private String oper_username; - - private String oper_id; - - private String account_no; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getAmount() { - return amount; - } - - public void setAmount(String amount) { - this.amount = amount; - } - - public String getRemark() { - return remark; - } - - public void setRemark(String remark) { - this.remark = remark; - } - - public String getOper_time() { - return oper_time; - } - - public void setOper_time(String oper_time) { - this.oper_time = oper_time; - } - - public String getOper_username() { - return oper_username; - } - - public void setOper_username(String oper_username) { - this.oper_username = oper_username; - } - - public String getOper_id() { - return oper_id; - } - - public void setOper_id(String oper_id) { - this.oper_id = oper_id; - } - - public String getAccount_no() { - return account_no; - } - - public void setAccount_no(String account_no) { - this.account_no = account_no; - } - - @Override - public String toString() { - return "ClassPojo [id = " + id + ", amount = " + amount + ", remark = " + remark + ", oper_time = " + oper_time + ", oper_username = " + oper_username + ", oper_id = " + oper_id + ", account_no = " + account_no + "]"; - } -} diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/AccountService.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/AccountService.java index 4990325..e9bc495 100644 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/AccountService.java +++ b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/AccountService.java @@ -2,6 +2,8 @@ import com.padeoe.nicservice.njuwlan.utils.NetworkUtils; +import java.io.IOException; + /** * 该类用于实现南京大学网络认证系统的账户管理功能,包括修改密码功能 * Created by padeoe on 2015/9/26. @@ -17,8 +19,8 @@ public class AccountService { * @param newPassword 南京大学网络认证系统的新密码 * @return 南京大学网络认证系统的服务器返回消息,可以通过 {@link #isChangePasswSuccess(String)}来对返回值分析是否改密成功 */ - public static String changePassword(String username, String oldPassword, String newPassword) { - if (LoginService.getInstance().isPortalOnline()) { + public static String changePassword(String username, String oldPassword, String newPassword) throws IOException { + if (new OnlineQueryService().isPortalOnline()) { return NetworkUtils.connectAndPost("oldpassword=" + oldPassword + "&newpassword=" + newPassword + "&confirmpassword=" + newPassword, "http://"+NetworkUtils.getCurrentPortalIP()+"/portal_io/selfservice/modifypassword", 200); } else { //首先登陆bras.nju.edu.cn获取cookie diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/DetailQuery.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/DetailQuery.java index 706b35e..9b4c078 100644 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/DetailQuery.java +++ b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/DetailQuery.java @@ -1,5 +1,7 @@ package com.padeoe.nicservice.njuwlan.service; +import java.io.IOException; + /** * 该接口用于描述南京大学网络认证系统的和南京大学网络认证计费系统自助平台都提供了的某些查询功能, * 包括获取当前在线设备,获取详单信息,获取认证信息,获取账单信息,获取充值信息 @@ -7,10 +9,10 @@ * Date: 2015/9/24 */ public interface DetailQuery { - String queryBy(int catalog, int page, int row, boolean order); - String getOnline(int page, int row); - String getAuthLog(int page, int row, boolean order); - String getAcct(int page, int row, boolean order); - String getBills(int page, int row, boolean order); - String getRecharge(int page, int row, boolean order); + String queryBy(int catalog, int page, int row, boolean order) throws IOException; + String getOnline(int page, int row) throws IOException; + String getAuthLog(int page, int row, boolean order) throws IOException; + String getAcct(int page, int row, boolean order) throws IOException; + String getBills(int page, int row, boolean order) throws IOException; + String getRecharge(int page, int row, boolean order) throws IOException; } diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/LoginService.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/LoginService.java index f663683..90a37dc 100644 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/LoginService.java +++ b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/LoginService.java @@ -2,6 +2,7 @@ import com.padeoe.nicservice.njuwlan.utils.NetworkUtils; +import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -45,7 +46,7 @@ public static LoginService getInstance() { * * @return challenge,null时失败 */ - private String[] getChallenge() { + private String[] getChallenge() throws IOException { String result = NetworkUtils.connectAndPost("", "http://" + getPortalIP() + "/portal_io/getchallenge", timeout); if (result != null && result.startsWith("{\"reply_msg\":\"操作成功\"")) { return new String[]{result.substring(result.indexOf("\"challenge\":\"") + 13, result.indexOf("\",\"reply_code\"")), result}; @@ -63,7 +64,7 @@ private String[] getChallenge() { * @return 返回的字符串的JSON解析 */ @Deprecated - public String oldConnect(String username, String password, int timeout) { + public String oldConnect(String username, String password, int timeout) throws IOException { String postdata = "action=login&username=" + username + "&password=" + password; return NetworkUtils.connectAndPost(postdata, "http://" + getPortalIP() + "/portal_io/login", timeout); } @@ -75,7 +76,7 @@ public String oldConnect(String username, String password, int timeout) { * @param password 南京大学网络认证系统的密码 * @return 服务器返回数据 */ - public String connect(String username, String password) { + public String connect(String username, String password) throws IOException { String challenge[] = getChallenge(); if (challenge[0] != null) { String postdata = "username=" + username + "&password=" + createChapPassword(password, challenge[0]) + "&challenge=" + challenge[0]; @@ -91,7 +92,7 @@ public String connect(String username, String password) { * * @return 服务器返回的数据 */ - public String disconnect() { + public String disconnect() throws IOException { return NetworkUtils.connectAndPost("", "http://" + getPortalIP() + "/portal_io/logout", timeout); } @@ -174,16 +175,6 @@ public static boolean isLogoutSuccess(String result) { return result.startsWith("{\"reply_code\":101"); } - /** - * 查询当前是否已登录南京大学网络认证系统 - * - * @return 表示当前是否在线 - */ - public boolean isPortalOnline() { - String result = NetworkUtils.connectAndPost("", "http://" + getPortalIP() + "/portal_io/getinfo", 200); - return result.endsWith("\"reply_code\":0,\"reply_msg\":\"操作成功\"}\n"); - } - /** * 获取缓存的南京大学网络认证系统的IP * diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OfflineQueryService.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OfflineQueryService.java index c461be5..14d7cd8 100644 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OfflineQueryService.java +++ b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OfflineQueryService.java @@ -5,6 +5,7 @@ import com.padeoe.nicservice.njuwlan.utils.NetworkUtils; import com.padeoe.utils.LoginException; +import java.io.IOException; import java.util.Calendar; /** @@ -32,7 +33,7 @@ public class OfflineQueryService implements DetailQuery { * @param password 南京大学网络认证计费系统自助平台的密码 * @throws LoginException 登陆失败 */ - public OfflineQueryService(String username, String password) throws LoginException { + public OfflineQueryService(String username, String password) throws LoginException, IOException { authkey = "username=" + username + "&password=" + password; resetCookie(authkey); } @@ -45,7 +46,7 @@ public OfflineQueryService(String username, String password) throws LoginExcepti * @param row 每页行数 * @return 服务器返回的json格式的数据 */ - public String queryBy(int catalog, int page, int row) { + public String queryBy(int catalog, int page, int row) throws IOException { return queryBy(catalog, page, row, true); } @@ -59,7 +60,7 @@ public String queryBy(int catalog, int page, int row) { * @return 服务器返回的json格式的数据 */ @Override - public String queryBy(int catalog, int page, int row, boolean order) { + public String queryBy(int catalog, int page, int row, boolean order) throws IOException { switch (catalog) { case ONLINE: return getOnline(page, row); @@ -76,61 +77,61 @@ public String queryBy(int catalog, int page, int row, boolean order) { } @Override - public String getOnline(int page, int row) { + public String getOnline(int page, int row) throws IOException { return NetworkUtils.postWithCookie("page=" + page + "&rows=" + row, cookie, "http://" + getBrasIP() + ":8080/manage/self/online/getlist", timeout); } @Override - public String getAuthLog(int page, int row, boolean order) { + public String getAuthLog(int page, int row, boolean order) throws IOException { return NetworkUtils.postWithCookie("sort=datetime&order=" + (order ? "desc" : "asc") + "&page=" + page + "&rows=" + row, cookie, "http://" + getBrasIP() + ":8080/manage/self/authlog/getlist", timeout); } - public String getAuthLog(int page, int row) { + public String getAuthLog(int page, int row) throws IOException { return getAuthLog(page, row, true); } @Override - public String getAcct(int page, int row, boolean order) { + public String getAcct(int page, int row, boolean order) throws IOException { int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1; return getAcct(currentMonth, page, row, order); } - public String getAcct(int month, int page, int row, boolean order) { + public String getAcct(int month, int page, int row, boolean order) throws IOException { return NetworkUtils.postWithCookie("month=" + month + "&sort=acctstarttime&order=" + (order ? "desc" : "asc") + "&page=" + page + "&rows=" + row, cookie, "http://" + getBrasIP() + ":8080/manage/self/detail/getlist", timeout); } - public String getAcct(int page, int row) { + public String getAcct(int page, int row) throws IOException { return getAcct(page, row, true); } @Override - public String getBills(int page, int row, boolean order) { + public String getBills(int page, int row, boolean order) throws IOException { return NetworkUtils.postWithCookie("sort=createtime&order=" + (order ? "DESC" : "ASC") + "&page=" + page + "&rows=" + row, cookie, "http://" + getBrasIP() + ":8080/manage/self/bill/getlist", timeout); } - public String getBills(int page, int row) { + public String getBills(int page, int row) throws IOException { return getBills(page, row, true); } @Override - public String getRecharge(int page, int row, boolean order) { + public String getRecharge(int page, int row, boolean order) throws IOException { return NetworkUtils.postWithCookie("sort=oper_time&order=" + (order ? "DESC" : "ASC") + "&page=" + page + "&rows=" + row, cookie, "http://" + getBrasIP() + ":8080/manage/self/recharge/getlist", timeout); } - public String getRecharge(int page, int row) { + public String getRecharge(int page, int row) throws IOException { return getRecharge(page, row, true); } - public void resetCookie(String authkey) throws LoginException { + public void resetCookie(String authkey) throws LoginException, IOException { String[] returnData = login(authkey, timeout); cookie = returnData[1]; } - public static String[] login(String username, String password, int timeout) throws LoginException { + public static String[] login(String username, String password, int timeout) throws LoginException, IOException { return login("username=" + username + "&password=" + password, timeout); } - public static String[] login(String authkey, int timeout) throws LoginException { + public static String[] login(String authkey, int timeout) throws LoginException, IOException { String[] result = NetworkUtils.postAndGetCookie(authkey, "http://" + getBrasIP() + ":8080/manage/self/auth/login", timeout); if (result[0].endsWith("\"reply_code\":0}\n")) { return result; @@ -204,12 +205,12 @@ public void setTimeout(int timeout) { this.timeout = timeout; } - public static BrasIDInfo getBrasIDInfo(String cookie) { + public static BrasIDInfo getBrasIDInfo(String cookie) throws IOException { String result = NetworkUtils.postWithCookie(null, cookie, "http://bras.nju.edu.cn:8080/manage/self/userinfo/getinfo", 1000); return BrasIDInfo.getFromJson(result); } - public static AuthLogBras getAuthLog(int page, int row, boolean order, String cookie) { + public static AuthLogBras getAuthLog(int page, int row, boolean order, String cookie) throws IOException { String result = NetworkUtils.postWithCookie("sort=datetime&order=" + (order ? "desc" : "asc") + "&page=" + page + "&rows=" + row, cookie, "http://" + "bras.nju.edu.cn" + ":8080/manage/self/authlog/getlist", 300); return AuthLogBras.getFromJson(result); } diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OnlineQueryService.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OnlineQueryService.java index 6f26e5d..eb4ecf1 100644 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OnlineQueryService.java +++ b/app/src/main/java/com/padeoe/nicservice/njuwlan/service/OnlineQueryService.java @@ -2,6 +2,8 @@ import com.padeoe.nicservice.njuwlan.utils.NetworkUtils; +import java.io.IOException; + /** * 该类实现了南京大学网络认证系统提供的各类查询功能。 * 包括查询用户信息,当前在线设备,详单信息,认证信息,充值信息,账单信息等。 @@ -28,7 +30,7 @@ public class OnlineQueryService implements DetailQuery { * @param row 每页行数 * @return 服务器返回的查询结果,json格式 */ - public String queryBy(int catalog, int page, int row) { + public String queryBy(int catalog, int page, int row) throws IOException { return queryBy(catalog, page, row,true); } @@ -42,7 +44,7 @@ public String queryBy(int catalog, int page, int row) { * @return 服务器返回的查询结果,json格式 */ @Override - public String queryBy(int catalog, int page, int row, boolean order) { + public String queryBy(int catalog, int page, int row, boolean order) throws IOException { switch (catalog) { case ONLINE: return getOnline(page, row); @@ -66,7 +68,7 @@ public String queryBy(int catalog, int page, int row, boolean order) { * @return 服务器返回的查询结果,json格式 */ @Override - public String getOnline(int page, int row) { + public String getOnline(int page, int row) throws IOException { return NetworkUtils.connectAndPost("page=" + page + "&rows=" + row, "http://"+getPortalIP()+"/portal_io/selfservice/online/getlist", timeout); } @@ -76,18 +78,10 @@ public String getOnline(int page, int row) { * * @return 服务器返回的查询结果,json格式 */ - public String getBasicInfo() { + public String getBasicInfo() throws IOException { return NetworkUtils.connectAndPost("", "http://"+getPortalIP()+"/portal_io/selfservice/volume/getlist", timeout); } - /** - * 获取UserInfo - * - * @return 服务器返回的查询结果,json格式 - */ - public String getUserInfo() { - return NetworkUtils.connectAndPost("", "http://"+getPortalIP()+"/portal_io/getinfo", timeout); - } /** * 获取登陆历史 * @@ -95,7 +89,7 @@ public String getUserInfo() { * @param row 每页行数 * @return 服务器返回的查询结果,json格式 */ - public String getAuthLog(int page, int row) { + public String getAuthLog(int page, int row) throws IOException { return getAuthLog(page, row, true); } @@ -108,7 +102,7 @@ public String getAuthLog(int page, int row) { * @return 服务器返回的查询结果,json格式 */ @Override - public String getAuthLog(int page, int row, boolean order) { + public String getAuthLog(int page, int row, boolean order) throws IOException { return NetworkUtils.connectAndPost("sort=id&order=" + (order ? "desc" : "asc") + "&page=" + page + "&rows=" + row, "http://"+getPortalIP()+"/portal_io/selfservice/authlog/getlist", timeout); } @@ -119,7 +113,7 @@ public String getAuthLog(int page, int row, boolean order) { * @param row 每页行数 * @return 服务器返回的查询结果,json格式 */ - public String getAcct(int page, int row) { + public String getAcct(int page, int row) throws IOException { return getAcct(page, row, true); } @@ -132,7 +126,7 @@ public String getAcct(int page, int row) { * @return 服务器返回的查询结果,json格式 */ @Override - public String getAcct(int page, int row, boolean order) { + public String getAcct(int page, int row, boolean order) throws IOException { return NetworkUtils.connectAndPost("sort=acctstoptime&order=" + (order ? "desc" : "asc") + "&page=" + page + "&rows=" + row, "http://"+getPortalIP()+"/portal_io/selfservice/acct/getlist", timeout); } @@ -143,7 +137,7 @@ public String getAcct(int page, int row, boolean order) { * @param row 每页行数 * @return 服务器返回的查询结果,json格式 */ - public String getBills(int page, int row) { + public String getBills(int page, int row) throws IOException { return getBills(page, row, true); } @@ -156,7 +150,7 @@ public String getBills(int page, int row) { * @return 服务器返回的查询结果,json格式 */ @Override - public String getBills(int page, int row, boolean order) { + public String getBills(int page, int row, boolean order) throws IOException { return NetworkUtils.connectAndPost("sort=id&order=" + (order ? "desc" : "asc") + "&page=" + page + "&rows=" + row, "http://"+getPortalIP()+"/portal_io/selfservice/bill/getlist", timeout); } @@ -167,7 +161,7 @@ public String getBills(int page, int row, boolean order) { * @param row 每页行数 * @return 服务器返回的查询结果,json格式 */ - public String getRecharge(int page, int row) { + public String getRecharge(int page, int row) throws IOException { return getRecharge(page, row, true); } @@ -180,10 +174,36 @@ public String getRecharge(int page, int row) { * @return 服务器返回的查询结果,json格式 */ @Override - public String getRecharge(int page, int row, boolean order) { + public String getRecharge(int page, int row, boolean order) throws IOException { return NetworkUtils.connectAndPost("sort=id&desc=" + (order ? "desc" : "asc") + "&page=" + page + "&rows=" + row, "http://"+getPortalIP()+"/portal_io/selfservice/recharge/getlist", timeout); } + /** + * 查询当前设备登录了南京大学网络认证系统的用户信息,如果没登陆,将返回错误提示 + * @return 服务器返回的查询结果,json格式,可被{@link com.padeoe.nicservice.njuwlan.object.portal.ReturnData#getFromJson(String)}解析为对象 + */ + public String getCurrentUserInfo() throws IOException { + return NetworkUtils.connectAndPost("", "http://" + getPortalIP() + "/portal_io/getinfo", 200); + } + + /** + * 查询当前是否已登录南京大学网络认证系统 + * + * @return 表示当前是否在线 + */ + public boolean isPortalOnline() throws IOException { + return isPortalOnline(getCurrentUserInfo()); + } + + /** + * 根据已有的服务器返回信息查询当前是否已登录南京大学网络认证系统 + * @param result 调用{@link #getCurrentUserInfo()} 查询获得的结果 + * @return 表示当前是否在线 + */ + public static boolean isPortalOnline(String result) { + return result.endsWith("\"reply_code\":0,\"reply_msg\":\"操作成功\"}\n"); + } + /** * 根据返回消息判断是否查询成功 * diff --git a/app/src/main/java/com/padeoe/nicservice/njuwlan/utils/NetworkUtils.java b/app/src/main/java/com/padeoe/nicservice/njuwlan/utils/NetworkUtils.java index 7142bf4..5ebcbab 100644 --- a/app/src/main/java/com/padeoe/nicservice/njuwlan/utils/NetworkUtils.java +++ b/app/src/main/java/com/padeoe/nicservice/njuwlan/utils/NetworkUtils.java @@ -2,6 +2,7 @@ import com.padeoe.utils.network.MyHttpRequest; +import java.io.IOException; import java.net.InetAddress; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -34,7 +35,7 @@ public class NetworkUtils { *@param timeout 超时时间 * @return Post返回的数据 */ - public static String connectAndPost(String postData, String URL, int timeout) { + public static String connectAndPost(String postData, String URL, int timeout) throws IOException { return MyHttpRequest.post(postData, URL, null, "UTF-8", "UTF-8", timeout); } @@ -46,7 +47,7 @@ public static String connectAndPost(String postData, String URL, int timeout) { * @param timeout 超时时间 * @return 服务器返回的数据 */ - public static String postWithCookie(String postData, String cookie, String URL, int timeout) { + public static String postWithCookie(String postData, String cookie, String URL, int timeout) throws IOException { return MyHttpRequest.postWithCookie(postData, URL, null, cookie, "UTF-8", "UTF-8", timeout); } @@ -57,7 +58,7 @@ public static String postWithCookie(String postData, String cookie, String URL, * @param timeout 超时时间 * @return 服务器返回的数据 */ - public static String[] postAndGetCookie(String postData, String URL, int timeout) { + public static String[] postAndGetCookie(String postData, String URL, int timeout) throws IOException { return MyHttpRequest.postAndGetCookie(postData, URL, null, "UTF-8", "UTF-8", timeout); } diff --git a/app/src/main/java/com/padeoe/njunet/connect/MainActivity.java b/app/src/main/java/com/padeoe/njunet/connect/MainActivity.java index ec2b6eb..656b972 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/MainActivity.java +++ b/app/src/main/java/com/padeoe/njunet/connect/MainActivity.java @@ -33,11 +33,14 @@ import android.widget.TextView; import android.widget.Toast; +import com.padeoe.nicservice.njuwlan.object.portal.ReturnData; +import com.padeoe.nicservice.njuwlan.object.portal.UserInfo; import com.padeoe.njunet.App; import com.padeoe.njunet.R; import com.padeoe.njunet.connect.controller.ConnectManager; import com.padeoe.njunet.connect.controller.ConnectService; +import com.padeoe.njunet.connect.controller.UpdateInfo; import com.padeoe.njunet.connect.uihandler.ConnectResultHandle; import com.padeoe.njunet.deploy.FirstSettingActivity; import com.padeoe.njunet.settings.MySettingsActivity; @@ -131,26 +134,33 @@ public void onClick(View view) { location = (TextView) findViewById(R.id.location); ip = (TextView) findViewById(R.id.ip); status_internet = (TextView) findViewById(R.id.status_internet); - WifiManager wifiManager = (WifiManager) App.getAppContext().getSystemService(Context.WIFI_SERVICE); - setNetInfo(wifiManager.getConnectionInfo().getSSID()); } - public void setNetInfo(String ssid) { - netinfo.setText(getNetInfo(ssid)); - } - - private SpannableString getNetInfo(String ssid) { + public void setNetInfo(UserInfo userInfo, String ssid) { if (ssid == null) { - return new SpannableString(getResources().getString(R.string.no_wifi)); + netinfo.setText(new SpannableString(getResources().getString(R.string.no_wifi))); } else { StringBuilder builder = new StringBuilder(); - String type = App.isInPortalWiFiSet(ssid) || App.isInSuspiciousWiFiSSIDSet(ssid) ? getResources().getString(R.string.NJUWLAN) : getResources().getString(R.string.Unknown_WLAN); - builder.append(type).append("\n").append(ssid); + builder.append(userInfo.getFullname()).append('(').append(userInfo.getUsername()).append(')').append("\n").append(ssid); String status_internet_str = builder.toString(); SpannableString spannableString = new SpannableString(status_internet_str); spannableString.setSpan(new StyleSpan(Typeface.ITALIC), status_internet_str.indexOf(ssid), status_internet_str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new RelativeSizeSpan(0.7f), status_internet_str.indexOf(ssid), status_internet_str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - return spannableString; + netinfo.setText(spannableString); + } + } + + public void setNetInfo(String title, String subtitle) { + if (subtitle == null) { + netinfo.setText(title); + } else { + StringBuilder builder = new StringBuilder(); + builder.append(title).append('\n').append(subtitle); + String status_internet_str = builder.toString(); + SpannableString spannableString = new SpannableString(status_internet_str); + spannableString.setSpan(new StyleSpan(Typeface.ITALIC), status_internet_str.indexOf('\n'), status_internet_str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + spannableString.setSpan(new RelativeSizeSpan(0.7f), status_internet_str.indexOf('\n'), status_internet_str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + netinfo.setText(spannableString); } } @@ -236,17 +246,25 @@ public View getStatusView(ConnectManager.Status status) { public void initData() { showProgress(); - ConnectManager connectManager = new ConnectManager(); - connectManager.addObserver(this); - connectManager.backgrConnect(); + + if (PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).getBoolean("login_on_activity_start", true)) { + ConnectManager connectManager = new ConnectManager(); + connectManager.addObserver(this); + connectManager.backgrConnect(); + } else { + System.out.println("即将获得当前是否登陆的数据"); + UpdateInfo updateInfo = new UpdateInfo(); + updateInfo.addObserver(this); + updateInfo.updateInfo(); + } + } @Override protected void onResume() { super.onResume(); - if (PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).getBoolean("login_on_activity_start", true)) { - initData(); - } + initData(); + } @Override diff --git a/app/src/main/java/com/padeoe/njunet/connect/controller/BackgrdTest.java b/app/src/main/java/com/padeoe/njunet/connect/controller/BackgrdTest.java deleted file mode 100644 index 0a1e09b..0000000 --- a/app/src/main/java/com/padeoe/njunet/connect/controller/BackgrdTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.padeoe.njunet.connect.controller; - -import android.app.AlarmManager; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; -import android.support.v4.content.LocalBroadcastManager; - -import com.padeoe.njunet.App; -import com.padeoe.njunet.connect.uihandler.Reachable; -import com.padeoe.njunet.util.MyObservable; -import com.padeoe.njunet.util.MyObserver; - -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * Created by padeoe on 2016/5/18. - */ -public class BackgrdTest implements MyObserver { - /*用于控制上一个连接没有结束时不再发起新的连接*/ - static AtomicBoolean isRunning = new AtomicBoolean(false); - public BackgrdTest(){} - public void start(){ - if(isRunning.compareAndSet(false,true)){ - System.out.println("正在定义闹钟"); - Intent intent = new Intent(App.getAppContext(), ConnectService.class); - intent.setAction(ConnectService.CONNECTY_TEST); - // Create a PendingIntent to be triggered when the alarm goes off - final PendingIntent pIntent = PendingIntent.getService(App.getAppContext(),0, intent, PendingIntent.FLAG_UPDATE_CURRENT); - AlarmManager alarm = (AlarmManager) App.getAppContext().getSystemService(Context.ALARM_SERVICE); - // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP - // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY - alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis()+2000, - 10000, pIntent); - } - - } - public void end(){ - if(isRunning.compareAndSet(true,false)){ - Intent intent = new Intent(App.getAppContext(), ConnectService.class); - intent.setAction(ConnectService.CONNECTY_TEST); - final PendingIntent pIntent = PendingIntent.getService(App.getAppContext(),0, intent, PendingIntent.FLAG_UPDATE_CURRENT); - AlarmManager alarm = (AlarmManager) App.getAppContext().getSystemService(Context.ALARM_SERVICE); - alarm.cancel(pIntent); - } - - } - - public void startTest(){ - ConnectManager connectManager=new ConnectManager(); - connectManager.addObserver(this); - connectManager.networkTest(); - } - - @Override - public void update(MyObservable myObservable, Reachable data) { - //更改全局状态 - ConnectManager.setStatus(data.isInternetAvailable()?ConnectManager.Status.ONLINE:ConnectManager.Status.OFFLINE); - //发送广播,通知其他界面更新信息 - Intent intent = new Intent(ConnectManager.NETWORK_TEST); - intent.putExtra("NETWORK_TEST", data); - LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(App.getAppContext()); - broadcastManager.sendBroadcast(intent); - } -} diff --git a/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectManager.java b/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectManager.java index 5d8e791..9466706 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectManager.java +++ b/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectManager.java @@ -1,18 +1,22 @@ package com.padeoe.njunet.connect.controller; import android.content.Context; +import android.content.Intent; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; +import android.preference.PreferenceManager; import android.util.Log; +import com.padeoe.nicservice.njuwlan.object.bras.list.OnlineBras; import com.padeoe.nicservice.njuwlan.object.portal.BasicInfo; import com.padeoe.nicservice.njuwlan.object.portal.ReturnData; import com.padeoe.nicservice.njuwlan.object.portal.row.BasicInfoRow; import com.padeoe.nicservice.njuwlan.service.LoginService; +import com.padeoe.nicservice.njuwlan.service.OfflineQueryService; import com.padeoe.nicservice.njuwlan.service.OnlineQueryService; import com.padeoe.nicservice.njuwlan.utils.NetworkUtils; import com.padeoe.njunet.App; @@ -31,7 +35,9 @@ import com.padeoe.njunet.connect.uihandler.WifiLostHandle; import com.padeoe.njunet.util.MyObservable; import com.padeoe.njunet.util.NetworkTest; +import com.padeoe.utils.LoginException; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -43,8 +49,6 @@ public class ConnectManager extends MyObservable { public static final String BACKGROUND_LOGIN_ACTION = "com.padeoe.njunet.BACKGROUND_LOGIN_ACTION"; public static final String WIFI_LOST_ACTION = "com.padeoe.njunet.WIFI_LOST_ACTION"; - public static final String NOT_TARGET_ACTION = "com.padeoe.njunet.NOT_TARGET_ACTION"; - public static final String NETWORK_TEST = "com.padeoe.njunet.NETWORK_TEST_ACTION"; public static final String UNKNOWNNET_ACTION = "com.padeoe.njunet.UNKNOWNNET_ACTION"; public static final String AUTHFAIL_ACTION = "com.padeoe.njunet.AUTHFAIL_ACTION"; public static final String WIFI_AVAILABLE_ACTION = "com.padeoe.njunet.WIFI_AVAILABLE_ACTION"; @@ -66,18 +70,28 @@ public enum Status {ONLINE, OFFLINE, WIFI_LOST, DETECTING} private static Status status = Status.OFFLINE; public void disconnectNow() { + Intent stopServiceIntent = new Intent(App.getAppContext(), ConnectService.class); + stopServiceIntent.setAction(ConnectService.STOP_SERVICE_ACTION); + App.getAppContext().startService(stopServiceIntent); if (isConnecting.compareAndSet(false, true)) { Thread thread; threadList.add(thread = new Thread() { @Override public void run() { // bindNetWork(null); - String result = LoginService.getInstance().disconnect(); - isConnecting.set(false); - ReturnData returnData = ReturnData.getFromJson(result); - setChanged(); - notifyObservers(returnData == null ? new ErrorHandle(result) : new OfflineHandle(returnData)); - threadList.remove(this); + String result; + try { + result = LoginService.getInstance().disconnect(); + isConnecting.set(false); + ReturnData returnData = ReturnData.getFromJson(result); + setChanged(); + notifyObservers(returnData == null ? new ErrorHandle(result) : new OfflineHandle(returnData)); + threadList.remove(this); + } catch (IOException e) { + isConnecting.set(false); + e.printStackTrace(); + } + } }); thread.start(); @@ -85,10 +99,76 @@ public void run() { } public void backgrConnect() { - backgrConnect(null); + if (/*ConnectService.isCheckBeforeLogin()*/false) { + checkOnline(); + } else { + login(); + } } - public void backgrConnect(final ConnectFailHandle connectFailHandle) { + //检查是否有其他设备在线,当前只处理学生标准及时服务 + public void checkOnline() { + if (isConnecting.compareAndSet(false, true)) { + Thread thread; + threadList.add(thread = new Thread() { + @Override + public void run() { + super.run(); + if (bindNetWork()) { + System.out.println("即将检查在线设备"); + try { + //首先查询当前设备是否已登录,如果已登录,则放弃登陆操作并刷新数据。如果未登录,查询其他设备是否登陆 + OnlineQueryService onlineQueryService = new OnlineQueryService(); + String result = onlineQueryService.getCurrentUserInfo(); + //如果已登录,则放弃登陆操作并刷新数据 + if (OnlineQueryService.isPortalOnline(result)) { + isConnecting.set(false); + setChanged(); + notifyObservers(new BackgrReturnDataHandle(result)); + return; + } else { + //当前设备没有登录,则查询其他设备是否登陆 + OfflineQueryService offlineQueryService = null; + offlineQueryService = new OfflineQueryService(ConnectService.getUsername(), ConnectService.getPassword()); + OnlineBras onlineBras = OnlineBras.getFromJson(offlineQueryService.getOnline(1, 20)); + isConnecting.set(false); + if (onlineBras != null) { + if (onlineBras.getOnlineRowBrases().length == 0) { + System.out.println("当前没有设备在线"); + login(); + } else { + System.out.println("当前已有其他设备在线"); + } + } + } + } catch (LoginException e) { + e.printStackTrace(); + setChanged(); + Log.e("bras登陆错误", e.getMessage()); + notifyObservers(new ErrorHandle(e.getMessage())); + } catch (IOException io) { + isConnecting.set(false); + io.printStackTrace(); + } + + + } else { + isConnecting.set(false); + onWiFiLost(); + } + threadList.remove(this); + } + }); + thread.start(); + } + } + + public void login() { + login(null); + } + + public void login(final ConnectFailHandle connectFailHandle) { + if (isConnecting.compareAndSet(false, true)) { Thread thread; threadList.add(thread = new Thread() { @@ -101,36 +181,36 @@ public void run() { setChanged(); notifyObservers(new NoPasswordHandle()); } else { - String result = LoginService.getInstance().connect(ConnectService.getUsername(), ConnectService.getPassword()); - isConnecting.set(false); - System.out.println("后台登陆结束" + result); - setChanged(); - if (LoginService.isLoginConnectSuccess(result)) { + String result = null; + try { + result = LoginService.getInstance().connect(ConnectService.getUsername(), ConnectService.getPassword()); + isConnecting.set(false); + System.out.println("后台登陆结束" + result); + setChanged(); saveCurrentSSID(); if (LoginService.isLoginSuccess(result)) { notifyObservers(new BackgrReturnDataHandle(result)); } else { notifyObservers(new AuthFailHandle(ReturnData.getFromJson(result))); } - } else { + } catch (IOException e) { + isConnecting.set(false); + System.out.println("后台登陆结束" + result); + setChanged(); + e.printStackTrace(); removeCurrentSSID(); if (connectFailHandle == null) { ConnectFailHandle newConnectFailHandle = new ConnectFailHandle(ConnectManager.this, 1); newConnectFailHandle.handle(); - // } else { connectFailHandle.handle(); // notifyObservers(connectFailHandle); } - } } } else { - setChanged(); - notifyObservers(new WifiLostHandle()); - Log.e("没有网络", "不应该进行连接操作"); - isConnecting.set(false); + onWiFiLost(); } threadList.remove(this); } @@ -159,10 +239,7 @@ public void run() { notifyObservers(new Reachable(-1)); } } else { - ConnectManager.setStatus(ConnectManager.Status.WIFI_LOST); - StatusNotificationManager.showStatus("未连接网络"); - Log.e("没有网络", "不应该进行连接操作"); - isConnecting.set(false); + onWiFiLost(); } threadList.remove(this); } @@ -170,39 +247,6 @@ public void run() { thread.start(); } - public void updateOnlineTime() { - Thread thread; - threadList.add(thread = new Thread() { - @Override - public void run() { - super.run(); - OnlineQueryService onlineQueryService = new OnlineQueryService(); - onlineQueryService.setTimeout(200); - String result = onlineQueryService.getBasicInfo(); - BasicInfo basicInfo = BasicInfo.getFromJson(result); - int seconds; - if (basicInfo != null && basicInfo.getBasicInfoRows() != null && basicInfo.getBasicInfoRows().length > 0) { - BasicInfoRow basicInfoRow = basicInfo.getBasicInfoRows()[0]; - try { - seconds = Integer.parseInt(basicInfoRow.getTotal_ipv4_volume()); - setChanged(); - notifyObservers(new OnlineTimeHandle(seconds)); - } catch (NumberFormatException e) { - setChanged(); - notifyObservers(new ErrorHandle(App.context.getString(R.string.illegal_result_from_server))); - } - } else { - ConnectManager.setStatus(ConnectManager.Status.WIFI_LOST); - StatusNotificationManager.showStatus("未连接网络"); - Log.e("没有网络", "不应该进行连接操作"); - isConnecting.set(false); - } - threadList.remove(this); - } - }); - thread.start(); - - } /** * 绑定wifi网络 @@ -246,13 +290,13 @@ public static boolean bindNetWork() { } - public static void saveCurrentSSID() { + private static void saveCurrentSSID() { WifiManager mWifi = (WifiManager) App.getAppContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = mWifi.getConnectionInfo(); App.addWiFiSSID(wifiInfo.getSSID()); } - public static void removeCurrentSSID() { + private static void removeCurrentSSID() { WifiManager mWifi = (WifiManager) App.getAppContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = mWifi.getConnectionInfo(); App.removeWiFiSSID(wifiInfo.getSSID()); @@ -279,4 +323,11 @@ public static String getInternetStatus() { return "unknown"; } } + + private static void onWiFiLost() { + ConnectManager.setStatus(ConnectManager.Status.WIFI_LOST); + StatusNotificationManager.showStatus("未连接网络"); + Log.e("没有网络", "不应该进行连接操作"); + isConnecting.set(false); + } } diff --git a/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectService.java b/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectService.java index f17d556..7832dea 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectService.java +++ b/app/src/main/java/com/padeoe/njunet/connect/controller/ConnectService.java @@ -12,11 +12,13 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.net.ConnectivityManager; +import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkRequest; import android.net.wifi.WifiManager; import android.os.Build; import android.os.IBinder; +import android.preference.PreferenceManager; import android.util.Log; import com.padeoe.njunet.App; @@ -25,13 +27,13 @@ import com.padeoe.njunet.util.PrefFileManager; /** - * 用于负责后台网络连接的服务,存储了网络连接相关的数据,包括用户名密码,目标SSID + * 用于负责后台网络连接的服务,存储了网络连接相关的数据,包括用户名密码 * 对于不同API等级,使用了不同的接口实现了网络状态监测 */ public class ConnectService extends Service { private static String username; private static String password; - private static String targetSSID; + private static Boolean checkBeforeLogin = null; Object wifiNetWorkCallBack; public static final String STOP_SERVICE_ACTION = "com.padeoe.njunet.STOP_SERVICE_ACTION"; public static final String SCAN_AND_CONNECT_ACTION = "com.padeoe.njunet.SCAN_AND_CONNECT_ACTION"; @@ -67,8 +69,10 @@ public void onCreate() { SharedPreferences sharedPreferences = PrefFileManager.getAccountPref(); username = sharedPreferences.getString("username", null); password = sharedPreferences.getString("password", null); - targetSSID = sharedPreferences.getString("target_SSID", null); - startMonitor(); + if (PreferenceManager.getDefaultSharedPreferences(App.getAppContext()).getBoolean("auto_connect", true)) { + startMonitor(); + } + } @Override @@ -92,9 +96,7 @@ public static String getUsername() { * @param username */ public static void setUsername(String username) { - SharedPreferences.Editor editor = PrefFileManager.getAccountPref().edit(); - ConnectService.username = username; - editor.putString("username", username).apply(); + PrefFileManager.getAccountPref().edit().putString("username", ConnectService.username = username).apply(); } /** @@ -112,78 +114,23 @@ public static String getPassword() { * @param password */ public static void setPassword(String password) { - SharedPreferences.Editor editor = PrefFileManager.getAccountPref().edit(); - ConnectService.password = password; - editor.putString("password", password).apply(); - } - - /** - * 获得识别校园网连接使用的SSID名称,若当前为空,则会从配置文件中查找 - */ - public static String getTargetSSID() { - return targetSSID != null ? targetSSID : (targetSSID = PrefFileManager.getAccountPref().getString("target_SSID", null)); + PrefFileManager.getAccountPref().edit().putString("password", ConnectService.password = password).apply(); } - /** - * 设置校园网连接的SSID,并写入配置文件 - * - * @param targetSSID - */ - public static void setTargetSSID(String targetSSID) { - SharedPreferences.Editor editor = PrefFileManager.getAccountPref().edit(); - ConnectService.targetSSID = targetSSID; - editor.putString("target_SSID", targetSSID).apply(); - } - - public static boolean isTargetWifi(String SSID) { - //如果目标wifi名称不存在就首先确定目标wifi名称 - if (getTargetSSID() == null) - ConnectService.setTargetSSID((Build.VERSION.SDK_INT >= 17 && SSID.startsWith("\"") && SSID.endsWith("\"")) ? "\"NJU-WLAN\"" : "NJU-WLAN"); - return SSID.equals(getTargetSSID()) || SSID.equals("NJU-FAST") || SSID.equals("\"NJU-FAST\""); - } private void startMonitor() { - //共有两个功能 - //1.后台连接p.nju.edu.cn - //2.检测portal网络并提示登录(仅Android N及以上具有该功能) - - //API<21 Android 4.0-4.4使用动态注册广播接收器的形式实现功能 - //API=21 Android 5.0使用public void requestNetwork (NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback)实现功能1 - //API=22 Android 6.0使用public void requestNetwork (NetworkRequest request, PendingIntent operation)实现功能1 - //API=23 Android N使用 public void registerNetworkCallback (NetworkRequest request, PendingIntent operation)实现功能1和功能2 - - if (Build.VERSION.SDK_INT <= 20) { + if (/*Build.VERSION.SDK_INT <= 20*/true) { registerWifiReceiver(); } - //开启后台自动连接项目 - if (Build.VERSION.SDK_INT >= 21) { + /** + * 本打算对API21采用注册网络回调{@link android.net.ConnectivityManager.NetworkCallback#registerNetworkCallback(NetworkRequest, ConnectivityManager.NetworkCallback)}的方法进行监听, + * 因为这样能方便的检查到网络的详细变化包括{@link android.net.ConnectivityManager.NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}等。然而首次打开或者后台被杀时,service重启再次打开回调时 + * 会导致{@link android.net.ConnectivityManager.NetworkCallback#onAvailable(Network)}方法可能被调用(wifi连接的情况下是一定会)(该方法中会调用登陆函数),所以用户会发现,本来点击下线已退出,然而杀掉后台后,又重新登录了。 + * + */ + if (/*Build.VERSION.SDK_INT >= 21*/false) { registerNetworkCallback(); } - - /* - if (Build.VERSION.SDK_INT == 22) { - Log.i("API22", "API22网络监测"); - NetworkRequest.Builder builder = new NetworkRequest.Builder(); - builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); - builder.addCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); - NetworkRequest wifiNetworkRequest = builder.build(); - ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); - PendingIntent pendingIntent = PendingIntent.getService(this, 233, new Intent(getApplicationContext(), ConnectService.class), PendingIntent.FLAG_UPDATE_CURRENT); - connectivityManager.requestNetwork(wifiNetworkRequest, pendingIntent); - } - - - //API23测试可用 - if (Build.VERSION.SDK_INT >= 23) { - Log.i("API23", "API23网络监测"); - NetworkRequest.Builder newbuilder = new NetworkRequest.Builder(); - newbuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); - newbuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); - NetworkRequest portalNetworkRequest = newbuilder.build(); - ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); - PendingIntent pendingIntent = PendingIntent.getService(this, 233, new Intent(getApplicationContext(), ConnectService.class), PendingIntent.FLAG_UPDATE_CURRENT); - connectivityManager.registerNetworkCallback(portalNetworkRequest, pendingIntent); - }*/ } private void endMonitor() { @@ -192,17 +139,14 @@ private void endMonitor() { } private void registerWifiReceiver() { - Log.i("API14", "API14网络监测"); networkConnectChangedReceiver = new NetworkConnectChangedReceiver(); IntentFilter filter = new IntentFilter(); - // filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); registerReceiver(networkConnectChangedReceiver, filter); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void registerNetworkCallback() { - Log.i("API21", "API21网络监测"); NetworkRequest.Builder builder = new NetworkRequest.Builder(); builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); NetworkRequest wifiNetworkRequest = builder.build(); @@ -239,6 +183,14 @@ private static boolean isServiceRunning(Class serviceClass) { public static boolean isRunning() { return isServiceRunning(ConnectService.class); } + + public static boolean isCheckBeforeLogin() { + return checkBeforeLogin != null ? checkBeforeLogin : (checkBeforeLogin = PreferenceManager.getDefaultSharedPreferences(App.getAppContext()).getBoolean("checkBeforeLogin", false)); + } + + public static void setCheckBeforeLogin(boolean checkBeforeLogin) { + PrefFileManager.getAccountPref().edit().putBoolean("checkBeforeLogin", ConnectService.checkBeforeLogin = checkBeforeLogin).apply(); + } } diff --git a/app/src/main/java/com/padeoe/njunet/connect/controller/UpdateInfo.java b/app/src/main/java/com/padeoe/njunet/connect/controller/UpdateInfo.java index 798076b..5a98a4e 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/controller/UpdateInfo.java +++ b/app/src/main/java/com/padeoe/njunet/connect/controller/UpdateInfo.java @@ -15,6 +15,8 @@ import com.padeoe.njunet.connect.uihandler.ReturnDataHandle; import com.padeoe.njunet.util.MyObservable; +import java.io.IOException; + /** * Created by padeoe on 2016/4/26. */ @@ -25,10 +27,17 @@ public void updateInfo() { public void run() { super.run(); OnlineQueryService onlineQueryService = new OnlineQueryService(); - String result = onlineQueryService.getUserInfo(); - ReturnData returnData = ReturnData.getFromJson(result); - setChanged(); - notifyObservers(returnData != null ? new ReturnDataHandle(returnData) : new ErrorHandle(result)); + try { + String result = onlineQueryService.getCurrentUserInfo(); + ReturnData returnData = ReturnData.getFromJson(result); + setChanged(); + notifyObservers(returnData != null ? new ReturnDataHandle(returnData) : new ErrorHandle(result)); + } catch (IOException e) { + setChanged(); + notifyObservers(new ErrorHandle(e.getMessage())); + e.printStackTrace(); + } + } }.start(); } @@ -39,25 +48,31 @@ public void updateOnlineTime() { public void run() { super.run(); OnlineQueryService onlineQueryService = new OnlineQueryService(); - String result = onlineQueryService.getBasicInfo(); - BasicInfo basicInfo = BasicInfo.getFromJson(result); - int seconds; - if (basicInfo != null && basicInfo.getBasicInfoRows() != null && basicInfo.getBasicInfoRows().length > 0) { - BasicInfoRow basicInfoRow = basicInfo.getBasicInfoRows()[0]; - try { - seconds = Integer.parseInt(basicInfoRow.getTotal_ipv4_volume()); - setChanged(); - notifyObservers(new OnlineTimeHandle(seconds)); - } catch (NumberFormatException e) { + String result = null; + try { + result = onlineQueryService.getBasicInfo(); + BasicInfo basicInfo = BasicInfo.getFromJson(result); + int seconds; + if (basicInfo != null && basicInfo.getBasicInfoRows() != null && basicInfo.getBasicInfoRows().length > 0) { + BasicInfoRow basicInfoRow = basicInfo.getBasicInfoRows()[0]; + try { + seconds = Integer.parseInt(basicInfoRow.getTotal_ipv4_volume()); + setChanged(); + notifyObservers(new OnlineTimeHandle(seconds)); + } catch (NumberFormatException e) { + setChanged(); + notifyObservers(new ErrorHandle(App.context.getString(R.string.illegal_result_from_server))); + } + } else { setChanged(); - notifyObservers(new ErrorHandle(App.context.getString(R.string.illegal_result_from_server))); + notifyObservers(new GetOnlineTimeFailHandle()); + Log.e("获取时间失败", result); } - } else { + } catch (IOException e) { setChanged(); - notifyObservers(new GetOnlineTimeFailHandle()); - Log.e("获取时间失败", result); + notifyObservers(new ErrorHandle(e.getMessage())); + e.printStackTrace(); } - } }.start(); } diff --git a/app/src/main/java/com/padeoe/njunet/connect/monitor/BootReceiver.java b/app/src/main/java/com/padeoe/njunet/connect/monitor/BootReceiver.java index d889d52..e8f67fe 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/monitor/BootReceiver.java +++ b/app/src/main/java/com/padeoe/njunet/connect/monitor/BootReceiver.java @@ -8,7 +8,9 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import com.padeoe.njunet.App; import com.padeoe.njunet.connect.controller.ConnectService; import com.padeoe.njunet.util.PrefFileManager; @@ -17,7 +19,8 @@ public class BootReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { SharedPreferences sharedPreferences = PrefFileManager.getAccountPref(); if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { - if (sharedPreferences.getBoolean("auto_connect", true) && + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("boot_launch", true) && + PreferenceManager.getDefaultSharedPreferences(context).getBoolean("auto_connect",true)&& sharedPreferences.getString("username", null) != null && sharedPreferences.getString("password", null) != null) { context.startService(new Intent(context, ConnectService.class)); diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/AuthFailHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/AuthFailHandle.java index b5e8ac3..531f43c 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/uihandler/AuthFailHandle.java +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/AuthFailHandle.java @@ -26,7 +26,7 @@ public AuthFailHandle(ReturnData returnData) { public void updateView(MainActivity activity) { activity.hideProgress(); WifiManager wifiManager = (WifiManager) App.getAppContext().getSystemService(Context.WIFI_SERVICE); - activity.setNetInfo(wifiManager.getConnectionInfo().getSSID()); + activity.setNetInfo("认证失败",wifiManager.getConnectionInfo().getSSID()); // activity.user_detail.setVisibility(View.VISIBLE); activity.updateViewStatus(activity.status, ConnectManager.getStatus()); activity.showOnMainActivity(returnData.getReply_message()); diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/ClientLimitHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/ClientLimitHandle.java new file mode 100644 index 0000000..45ba40a --- /dev/null +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/ClientLimitHandle.java @@ -0,0 +1,42 @@ +package com.padeoe.njunet.connect.uihandler; + +import android.os.Parcel; + +import com.padeoe.njunet.connect.MainActivity; + +/** + * Created by padeoe on 2016/6/3. + */ +public class ClientLimitHandle implements ConnectResultHandle { + @Override + public void updateView(MainActivity activity) { + + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + } + + public ClientLimitHandle() { + } + + protected ClientLimitHandle(Parcel in) { + } + + public static final Creator CREATOR = new Creator() { + @Override + public ClientLimitHandle createFromParcel(Parcel source) { + return new ClientLimitHandle(source); + } + + @Override + public ClientLimitHandle[] newArray(int size) { + return new ClientLimitHandle[size]; + } + }; +} diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/ConnectFailHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/ConnectFailHandle.java index f5da3f9..a2a3d09 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/uihandler/ConnectFailHandle.java +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/ConnectFailHandle.java @@ -50,7 +50,7 @@ public void handle() { } else { if (currentTimes < App.getMaxTry()) { currentTimes++; - connectManager.backgrConnect(this); + connectManager.login(this); } else { StatusNotificationManager.showStatus(); Log.e("ConnectFailHandle", "达到了最大尝试次数,判定为未知网路"); @@ -83,7 +83,7 @@ public void update(MyObservable myObservable, Reachable data) { connectManager.notifyObservers(new UnknownNetHandle(ssid)); } else { ConnectManager.setStatus(ConnectManager.Status.OFFLINE); - connectManager.backgrConnect(this); + connectManager.login(this); } } } diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/OfflineHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/OfflineHandle.java index 1827b74..cd2f9fd 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/uihandler/OfflineHandle.java +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/OfflineHandle.java @@ -1,12 +1,16 @@ package com.padeoe.njunet.connect.uihandler; +import android.content.Context; +import android.net.wifi.WifiManager; import android.os.Parcel; import android.os.Parcelable; import com.padeoe.nicservice.njuwlan.object.portal.ReturnData; +import com.padeoe.njunet.App; import com.padeoe.njunet.connect.MainActivity; import com.padeoe.njunet.connect.StatusNotificationManager; import com.padeoe.njunet.connect.controller.ConnectManager; +import com.padeoe.njunet.util.MyAnimation; /** * Created by padeoe on 2016/5/10. @@ -21,7 +25,8 @@ public OfflineHandle(ReturnData returnData) { @Override public void updateView(MainActivity mainActivity) { mainActivity.hideProgress(); - // mainActivity.user_detail.setVisibility(View.INVISIBLE); + mainActivity.setNetInfo("未登录",((WifiManager)App.getAppContext().getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getSSID()); + MyAnimation.fadeInTextView(mainActivity.netinfo); ConnectManager.setStatus(ConnectManager.Status.OFFLINE); StatusNotificationManager.showStatus(); mainActivity.updateViewStatus(mainActivity.status, ConnectManager.getStatus()); diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/ReturnDataHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/ReturnDataHandle.java index 7c739c4..86f5706 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/uihandler/ReturnDataHandle.java +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/ReturnDataHandle.java @@ -37,7 +37,8 @@ public void updateView(MainActivity mainActivity) { if (userinfo != null) { // mainActivity.username.setText(userinfo.getUsername() + "(" + userinfo.getFullname() + ")"); WifiManager wifiManager = (WifiManager) App.getAppContext().getSystemService(Context.WIFI_SERVICE); - mainActivity.setNetInfo(wifiManager.getConnectionInfo().getSSID()); + mainActivity.setNetInfo(userinfo,wifiManager.getConnectionInfo().getSSID()); + MyAnimation.fadeInTextView(mainActivity.netinfo); // mainActivity.user_detail.setVisibility(View.VISIBLE); mainActivity.amount.setText(Double.valueOf(userinfo.getBalance()) / 100 + "元"); mainActivity.location.setText(userinfo.getArea_name()); @@ -49,7 +50,9 @@ public void updateView(MainActivity mainActivity) { updateInfo.addObserver(mainActivity); updateInfo.updateOnlineTime(); } else { - ConnectManager.setStatus(ConnectManager.Status.OFFLINE); + //mainActivity.hideProgress(); + //ConnectManager.setStatus(ConnectManager.Status.OFFLINE); + new OfflineHandle(returnData).updateView(mainActivity); } mainActivity.updateViewStatus(mainActivity.status, ConnectManager.getStatus()); } diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/UnknownNetHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/UnknownNetHandle.java index 56ad8cb..040090b 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/uihandler/UnknownNetHandle.java +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/UnknownNetHandle.java @@ -2,6 +2,7 @@ import android.os.Parcel; +import com.padeoe.njunet.App; import com.padeoe.njunet.R; import com.padeoe.njunet.connect.MainActivity; import com.padeoe.njunet.connect.controller.ConnectManager; @@ -21,7 +22,8 @@ public UnknownNetHandle(String SSID) { public void updateView(MainActivity activity) { activity.hideProgress(); // activity.user_detail.setVisibility(View.VISIBLE); - activity.setNetInfo(SSID); + activity.setNetInfo(App.isInPortalWiFiSet(SSID) || App.isInSuspiciousWiFiSSIDSet(SSID) ? + App.getAppContext().getResources().getString(R.string.NJUWLAN) : App.getAppContext().getResources().getString(R.string.Unknown_WLAN), SSID); activity.updateViewStatus(activity.status, ConnectManager.getStatus()); MyAnimation.fadeInTextView(activity.status_internet); activity.showOnMainActivity(activity.getResources().getString(R.string.connect_fail)); diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiAvailableHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiAvailableHandle.java index 97cef0c..c684329 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiAvailableHandle.java +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiAvailableHandle.java @@ -2,6 +2,8 @@ import android.os.Parcel; +import com.padeoe.njunet.App; +import com.padeoe.njunet.R; import com.padeoe.njunet.connect.MainActivity; /** @@ -12,7 +14,8 @@ public class WifiAvailableHandle implements ConnectResultHandle { @Override public void updateView(MainActivity activity) { - activity.setNetInfo(ssid); + activity.setNetInfo(App.isInPortalWiFiSet(ssid) || App.isInSuspiciousWiFiSSIDSet(ssid) ? + App.getAppContext().getResources().getString(R.string.NJUWLAN) : App.getAppContext().getResources().getString(R.string.Unknown_WLAN), ssid); } @Override diff --git a/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiLostHandle.java b/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiLostHandle.java index f486941..fd5ce79 100644 --- a/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiLostHandle.java +++ b/app/src/main/java/com/padeoe/njunet/connect/uihandler/WifiLostHandle.java @@ -13,7 +13,7 @@ public class WifiLostHandle implements ConnectResultHandle { @Override public void updateView(MainActivity activity) { activity.hideProgress(); - activity.setNetInfo(null); + activity.setNetInfo("未连接WLAN",null); // activity.user_detail.setVisibility(View.INVISIBLE); ConnectManager.setStatus(ConnectManager.Status.WIFI_LOST); activity.updateViewStatus(activity.status, ConnectManager.getStatus()); diff --git a/app/src/main/java/com/padeoe/njunet/deploy/AccountInputFragment.java b/app/src/main/java/com/padeoe/njunet/deploy/AccountInputFragment.java index 296dd61..20ffa3f 100644 --- a/app/src/main/java/com/padeoe/njunet/deploy/AccountInputFragment.java +++ b/app/src/main/java/com/padeoe/njunet/deploy/AccountInputFragment.java @@ -18,6 +18,7 @@ import android.widget.TextView; import com.padeoe.njunet.R; +import com.padeoe.njunet.connect.controller.ConnectService; import com.padeoe.njunet.util.PrefFileManager; public class AccountInputFragment extends DeployFragment { @@ -37,7 +38,6 @@ public void onCreate(Bundle savedInstanceState) { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - // Inflate the layout for this fragment Button button = (Button) (getActivity().findViewById(R.id.nextStepButton)); button.setText(getNextStepButtonText()); view = inflater.inflate(R.layout.fragment_account_input, container, false); @@ -83,10 +83,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { @Override public void handle(View view) { - String username = getUserName(); - String password = getPassword(); - SharedPreferences.Editor editor = PrefFileManager.getAccountPref().edit(); - editor.putString("username", username).putString("password", password).putBoolean("isFirstInstall", false).apply(); + ConnectService.setUsername(getUserName()); + ConnectService.setPassword(getPassword()); view.setEnabled(true); } diff --git a/app/src/main/java/com/padeoe/njunet/deploy/FirstSettingActivity.java b/app/src/main/java/com/padeoe/njunet/deploy/FirstSettingActivity.java index 0d10a3d..e5e0be0 100644 --- a/app/src/main/java/com/padeoe/njunet/deploy/FirstSettingActivity.java +++ b/app/src/main/java/com/padeoe/njunet/deploy/FirstSettingActivity.java @@ -75,8 +75,8 @@ public boolean onOptionsItemSelected(MenuItem item) { public void onNextStepButtonClicked(View view) { Log.d("栈大小", fragmentManager.getBackStackEntryCount() + ""); int stackSize = fragmentManager.getBackStackEntryCount(); + fragments.get(stackSize).handle(view); if (stackSize < fragments.size() - 1) { - fragments.get(stackSize).handle(view); showNewFragment(fragments.get(stackSize + 1)); } else { Intent intent = getIntent(); diff --git a/app/src/main/java/com/padeoe/njunet/deploy/SettingFinishFragment.java b/app/src/main/java/com/padeoe/njunet/deploy/SettingFinishFragment.java index 07253aa..9af8c1f 100644 --- a/app/src/main/java/com/padeoe/njunet/deploy/SettingFinishFragment.java +++ b/app/src/main/java/com/padeoe/njunet/deploy/SettingFinishFragment.java @@ -9,15 +9,9 @@ import android.widget.Button; import com.padeoe.njunet.R; +import com.padeoe.njunet.util.PrefFileManager; + -/** - * A simple {@link Fragment} subclass. - * Activities that contain this fragment must implement the - * {@link SettingFinishFragment.OnFragmentInteractionListener} interface - * to handle interaction events. - * Use the {@link SettingFinishFragment#newInstance} factory method to - * create an instance of this fragment. - */ public class SettingFinishFragment extends DeployFragment { @Override @@ -37,6 +31,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, @Override public void handle(View view) { + PrefFileManager.getAccountPref().edit().putBoolean("isFirstInstall",false).apply(); } /** diff --git a/app/src/main/java/com/padeoe/service/DictService.java b/app/src/main/java/com/padeoe/service/DictService.java index 7b07541..abb3d08 100644 --- a/app/src/main/java/com/padeoe/service/DictService.java +++ b/app/src/main/java/com/padeoe/service/DictService.java @@ -2,6 +2,8 @@ import com.padeoe.utils.network.MyHttpRequest; +import java.io.IOException; + /** * Created by padeoe on 2015/12/13. */ @@ -11,7 +13,7 @@ public class DictService { * @param word 英语单词 * @return 单词解释的html页面 */ - public static String lookUp(String word) { + public static String lookUp(String word) throws IOException { return MyHttpRequest.post("word="+word,"http://bbs.nju.edu.cn/bbsdict?type=1",null,"UTF-8","GBK",500); } } diff --git a/app/src/main/java/com/padeoe/utils/network/MyHttpRequest.java b/app/src/main/java/com/padeoe/utils/network/MyHttpRequest.java index 2887015..b0880ce 100644 --- a/app/src/main/java/com/padeoe/utils/network/MyHttpRequest.java +++ b/app/src/main/java/com/padeoe/utils/network/MyHttpRequest.java @@ -12,71 +12,73 @@ /** * 该类用于负责http网络请求,包含get,set等方法 - * @author padeoe,Nifury - * Date: 2015/12/13 + * + * @author padeoe, Nifury + * Date: 2015/12/13 */ public class MyHttpRequest { /** * POST请求 - * @param action post或get请求 - * @param postData 数据 - * @param URL 服务器地址 + * + * @param action post或get请求 + * @param postData 数据 + * @param URL 服务器地址 * @param requestProperty 请求头 - * @param cookie cookie若无则置为空 - * @param inputEncoding 请求编码 - * @param outputEncoding 响应编码 - * @param timeout 超时时间 + * @param cookie cookie若无则置为空 + * @param inputEncoding 请求编码 + * @param outputEncoding 响应编码 + * @param timeout 超时时间 * @return 字符串数组,第一个元素是响应数据,若长度为2则第二个是返回的cookie */ - public static String[] action(String action,String postData,String URL,Map requestProperty,String cookie,String inputEncoding,String outputEncoding,int timeout){ - try { - byte[] postAsBytes=new byte[]{}; - if(postData!=null){ - postAsBytes= postData.getBytes(inputEncoding); - } - java.net.URL url = new URL(URL); - HttpURLConnection connection = (HttpURLConnection) url - .openConnection(); - connection.setConnectTimeout(timeout); - connection.setDoOutput(true); - connection.setRequestMethod(action); - connection.setUseCaches(false); + public static String[] action(String action, String postData, String URL, Map requestProperty, String cookie, String inputEncoding, String outputEncoding, int timeout) throws IOException { + byte[] postAsBytes = new byte[]{}; + if (postData != null) { + postAsBytes = postData.getBytes(inputEncoding); + } + java.net.URL url = new URL(URL); + HttpURLConnection connection = (HttpURLConnection) url + .openConnection(); + connection.setConnectTimeout(timeout); + connection.setDoOutput(true); + connection.setRequestMethod(action); + connection.setUseCaches(false); /* java 1.6 does not support requestProperty.forEach((k,v) -> connection.setRequestProperty(k, v)); */ - if(requestProperty!=null){ - for(Map.Entry entry : requestProperty.entrySet()) { - connection.setRequestProperty(entry.getKey(), entry.getValue()); - } + if (requestProperty != null) { + for (Map.Entry entry : requestProperty.entrySet()) { + connection.setRequestProperty(entry.getKey(), entry.getValue()); } - connection.setRequestProperty("Content-Length", String.valueOf(postAsBytes.length)); - if(cookie!=null){ - connection.setRequestProperty("Cookie", cookie); - } - connection.connect(); + } + connection.setRequestProperty("Content-Length", String.valueOf(postAsBytes.length)); + if (cookie != null) { + connection.setRequestProperty("Cookie", cookie); + } + connection.connect(); /* java 1.6 do not support try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(postAsBytes); }*/ - OutputStream outputStream = null; - try { - outputStream = connection.getOutputStream(); - outputStream.write(postAsBytes); - } finally { - if (outputStream != null) { - outputStream.close(); - } - + OutputStream outputStream = null; + try { + outputStream = connection.getOutputStream(); + outputStream.write(postAsBytes); + } finally { + if (outputStream != null) { + outputStream.close(); } - //读取返回数据 - MyByteArray myByteArray = new MyByteArray(); + } + + //读取返回数据 + MyByteArray myByteArray = new MyByteArray(); /* java 1.6 do not support try (InputStream inputStream = connection.getInputStream()) { len = inputStream.read(readData); }*/ - InputStream inputStream = null; + InputStream inputStream = null; + if (postData != null) { try { inputStream = connection.getInputStream(); while (true) { @@ -95,155 +97,112 @@ public static String[] action(String action,String postData,String URL,Map requestProperty,String inputEncoding,String outputEncoding,int timeout){ - return action("POST",postData,URL,requestProperty,null,inputEncoding,outputEncoding,timeout); + public static String[] postAndGetCookie(String postData, String URL, Map requestProperty, String inputEncoding, String outputEncoding, int timeout) throws IOException { + return action("POST", postData, URL, requestProperty, null, inputEncoding, outputEncoding, timeout); } - /**发送cookie的POST请求 + /** + * 发送cookie的POST请求 * - * @param postData 请求数据 - * @param URL 服务器地址 + * @param postData 请求数据 + * @param URL 服务器地址 * @param requestProperty 请求头 - * @param cookie 发送的cookie - * @param inputEncoding 请求编码 - * @param outputEncoding 响应编码 - * @param timeout 超时时间 + * @param cookie 发送的cookie + * @param inputEncoding 请求编码 + * @param outputEncoding 响应编码 + * @param timeout 超时时间 * @return 响应数据 */ - public static String postWithCookie(String postData,String URL,Map requestProperty,String cookie,String inputEncoding,String outputEncoding,int timeout){ - return action("POST",postData,URL,requestProperty,cookie,inputEncoding,outputEncoding,timeout)[0]; + public static String postWithCookie(String postData, String URL, Map requestProperty, String cookie, String inputEncoding, String outputEncoding, int timeout) throws IOException { + return action("POST", postData, URL, requestProperty, cookie, inputEncoding, outputEncoding, timeout)[0]; } /** * POST请求(不含cookie) - * @param postData 请求数据 - * @param URL 服务器地址 + * + * @param postData 请求数据 + * @param URL 服务器地址 * @param requestProperty 请求头 - * @param inputEncoding 请求编码 - * @param outputEncoding 响应编码 - * @param timeout 超时时间 + * @param inputEncoding 请求编码 + * @param outputEncoding 响应编码 + * @param timeout 超时时间 * @return 响应数据 */ - public static String post(String postData,String URL,Map requestProperty,String inputEncoding,String outputEncoding,int timeout){ - return action("POST",postData,URL,requestProperty,null,inputEncoding,outputEncoding,timeout)[0]; + public static String post(String postData, String URL, Map requestProperty, String inputEncoding, String outputEncoding, int timeout) throws IOException { + return action("POST", postData, URL, requestProperty, null, inputEncoding, outputEncoding, timeout)[0]; } /** * 获得cookie的POST请求 - * @param data 请求数据 - * @param URL 服务器地址 + * + * @param data 请求数据 + * @param URL 服务器地址 * @param requestProperty 请求头 - * @param inputEncoding 请求编码 - * @param outputEncoding 响应编码 - * @param timeout 超时时间 + * @param inputEncoding 请求编码 + * @param outputEncoding 响应编码 + * @param timeout 超时时间 * @return 字符串数组,第一个元素是响应数据,第二个是返回的cookie */ - public static String[] getAndGetCookie(String data,String URL,Map requestProperty,String inputEncoding,String outputEncoding,int timeout){ - return action("GET",data,URL,requestProperty,null,inputEncoding,outputEncoding,timeout); + public static String[] getAndGetCookie(String data, String URL, Map requestProperty, String inputEncoding, String outputEncoding, int timeout) throws IOException { + return action("GET", data, URL, requestProperty, null, inputEncoding, outputEncoding, timeout); } - /**发送cookie的POST请求 + /** + * 发送cookie的POST请求 * - * @param data 请求数据 - * @param URL 服务器地址 + * @param data 请求数据 + * @param URL 服务器地址 * @param requestProperty 请求头 - * @param cookie 发送的cookie - * @param inputEncoding 请求编码 - * @param outputEncoding 响应编码 - * @param timeout 超时时间 + * @param cookie 发送的cookie + * @param inputEncoding 请求编码 + * @param outputEncoding 响应编码 + * @param timeout 超时时间 * @return 响应数据 */ - public static String getWithCookie(String data,String URL,Map requestProperty,String cookie,String inputEncoding,String outputEncoding,int timeout){ - return action("GET",data,URL,requestProperty,cookie,inputEncoding,outputEncoding,timeout)[0]; + public static String getWithCookie(String data, String URL, Map requestProperty, String cookie, String inputEncoding, String outputEncoding, int timeout) throws IOException { + return action("GET", data, URL, requestProperty, cookie, inputEncoding, outputEncoding, timeout)[0]; } /** * POST请求(不含cookie) - * @param data 请求数据 - * @param URL 服务器地址 + * + * @param data 请求数据 + * @param URL 服务器地址 * @param requestProperty 请求头 - * @param inputEncoding 请求编码 - * @param outputEncoding 响应编码 - * @param timeout 超时时间 + * @param inputEncoding 请求编码 + * @param outputEncoding 响应编码 + * @param timeout 超时时间 * @return 响应数据 */ - public static String get(String data,String URL,Map requestProperty,String inputEncoding,String outputEncoding,int timeout){ - return action("GET",data,URL,requestProperty,null,inputEncoding,outputEncoding,timeout)[0]; - } - - public static int getReturnCode(String action,String postData,String URL,Map requestProperty,String inputEncoding,String outputEncoding,int timeout){ - try { - byte[] postAsBytes=new byte[]{}; - if(postData!=null){ - postAsBytes= postData.getBytes(inputEncoding); - } - java.net.URL url = new URL(URL); - HttpURLConnection connection = (HttpURLConnection) url - .openConnection(); - connection.setConnectTimeout(timeout); - connection.setDoOutput(true); - connection.setRequestMethod(action); - connection.setUseCaches(false); - /* java 1.6 does not support - requestProperty.forEach((k,v) -> connection.setRequestProperty(k, v)); - */ - if(requestProperty!=null){ - for(Map.Entry entry : requestProperty.entrySet()) { - connection.setRequestProperty(entry.getKey(), entry.getValue()); - } - } - connection.setRequestProperty("Content-Length", String.valueOf(postAsBytes.length)); - connection.connect(); - int code=connection.getResponseCode(); - connection.disconnect(); - return code; - } catch (UnsupportedEncodingException e) { - System.out.println(e); - return -1; - } catch (MalformedURLException malformedURLException) { - System.out.println(malformedURLException); - return -2; - } catch (ProtocolException protocolException) { - System.out.println(protocolException); - return -3; - } catch (IOException ioException) { - System.out.println(ioException); - return -4; - - } + public static String get(String data, String URL, Map requestProperty, String inputEncoding, String outputEncoding, int timeout) throws IOException { + return action("GET", data, URL, requestProperty, null, inputEncoding, outputEncoding, timeout)[0]; } } diff --git a/app/src/main/res/drawable-hdpi/open_source_icon.png b/app/src/main/res/drawable-hdpi/open_source_icon.png new file mode 100644 index 0000000..023de1d Binary files /dev/null and b/app/src/main/res/drawable-hdpi/open_source_icon.png differ diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 79cab20..06f6663 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -50,7 +50,7 @@ 1 当年点击连接登陆按钮时,如果WiFi未连接,将自动打开WiFi开关并扫描附近校园网WLAN - 打开app时刷新网络状态 + 每次打开app主界面时都进行一次立即登录 账号 连接WLAN并登录 刷新 diff --git a/app/src/main/res/values-zh/strings_pref.xml b/app/src/main/res/values-zh/strings_pref.xml index 46330d5..a742b79 100644 --- a/app/src/main/res/values-zh/strings_pref.xml +++ b/app/src/main/res/values-zh/strings_pref.xml @@ -4,7 +4,7 @@ 网络 通知 开机时启动后台服务 - 检测到校园网WLAN时 + 将在刚连接到校园网WLAN的瞬间进行一次自动登录 自动登录 登陆服务 网络参数 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 739084b..db6f181 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -52,7 +52,7 @@ 1 If the wifi is not connected When you press login button,the app will scan the wifi and connect to NJU WLAN nearby - refresh status on launching + login on launching Account Refresh wlan and login Refresh diff --git a/app/src/main/res/values/strings_pref.xml b/app/src/main/res/values/strings_pref.xml index 2cd57c1..75eb98d 100644 --- a/app/src/main/res/values/strings_pref.xml +++ b/app/src/main/res/values/strings_pref.xml @@ -4,7 +4,7 @@ Network Notifications launch service on boot - when connectted to NJU WLAN + login when connected to WLAN in NJU login automatically Login service Network parameters diff --git a/app/src/main/res/xml/pref_network.xml b/app/src/main/res/xml/pref_network.xml index 8958b3d..615be57 100644 --- a/app/src/main/res/xml/pref_network.xml +++ b/app/src/main/res/xml/pref_network.xml @@ -16,6 +16,7 @@ android:key="auto_connect" android:summary="@string/summary_auto_connect" android:title="@string/title_auto_connect" /> +