{"id":25891506,"url":"https://github.com/lengpucheng/android-java-zfeducation-system","last_synced_at":"2025-10-19T11:14:57.974Z","repository":{"id":41559043,"uuid":"256493534","full_name":"lengpucheng/Android-JAVA-ZFeducation-system","owner":"lengpucheng","description":"一个安卓正方教务系统小工具😊可以爬取课表等信息，并且设计了用户显示界面，按课程时间和周显示课表，默认为武汉纺织大学教务系统，可以在JWHelper中修改地址😄","archived":false,"fork":false,"pushed_at":"2020-11-30T01:23:02.000Z","size":10784,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-11-19T06:28:55.981Z","etag":null,"topics":["android","education-system","java","zhengfang"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lengpucheng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-17T12:13:41.000Z","updated_at":"2023-10-07T08:25:54.000Z","dependencies_parsed_at":"2022-09-21T13:23:56.906Z","dependency_job_id":null,"html_url":"https://github.com/lengpucheng/Android-JAVA-ZFeducation-system","commit_stats":null,"previous_names":[],"tags_count":1,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lengpucheng%2FAndroid-JAVA-ZFeducation-system","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lengpucheng%2FAndroid-JAVA-ZFeducation-system/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lengpucheng%2FAndroid-JAVA-ZFeducation-system/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lengpucheng%2FAndroid-JAVA-ZFeducation-system/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lengpucheng","download_url":"https://codeload.github.com/lengpucheng/Android-JAVA-ZFeducation-system/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241567031,"owners_count":19983414,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","education-system","java","zhengfang"],"created_at":"2025-03-02T20:28:26.859Z","updated_at":"2025-10-19T11:14:52.921Z","avatar_url":"https://github.com/lengpucheng.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\n# Android-JAVA-ZFeducation-system\n在安卓上使用JAVA-HttpClient5.0从正方教务系统上爬取课程表等相关信息，并且设计了用户显示界面，按课程时间和周显示课表\n\n### 目前已经实现了如下功能：\n+ 1、通过教务系统`判断学号密码`\n+ 2、模拟登录`抓取课程信息`\n+ 3、根据课程信息`显示课程表`\n+ 4、`动态判断是否本周`\n+ 5、自定义`添加`、`编辑`、`删除`课程\n+ 6、自动`判断课程导入`\n+ 7、`高亮显示当天`\n### 并且预留了以下实现接口：\n(*主要是一个人，一次性写完时间太长*）\n+ 1、通过教务系统`读取学籍信息`、`抓取照片`\n+ 2、`抓取成绩`\n+ 3、`空教室查询`\n+ 3、`POST选课`\n+ 4、`教学评价`\n+ 5、`新闻和通知获取`\n\n### 开发环境\n`Android Studio3.6` `JDK1.8` `API29`\n\n### 目录结构\n* `cn.edu.wtu.kcb`主包\n+ `activity` 活动\n   `OpenActivity`启动页面，判断是否已经有课表数据\n   `CourseActivity`显示课表\n   `MainActivity` 登录页面\n   `EditCourseActivity` 编辑/添加课程\n+ `db` 存放数据库相关\n* `model`存放对象\n+ `util`工具包\n   `JWhelper` 正方教务系统模拟登录\n### 测试DOME\n存放于app-debug中，在华为MENU上通过真机测试\n\n### 已知问题\n1、偶尔（非常少）会出现登录用户名、密码、验证码都正确但提示登录失败的情况；\n\n2、偶尔（经常）在重新导入课表时候不会删除之前的课表造成叠加。\n\n### 作者\nSigned-off-by: lengpucheng \u003clpc@hll520.cn\u003e\n\n## 2020/5/02日更新内容\n+ *更新了AES加密*  \n因为教务系统又一次进行了版本升级，将之前的明文存储更换为AES加密，故本次更新跟进\n\n## 2020/4/18日更新内容：\n+ *更新了项目结构，将方法按用途分包分类，便于维护和扩展*\n+ *优化了数据结构，将周和节变为`int MIN,MAX` 分别存储，便于扩展*\n\n\n## 2020/4/21日更新\n+ *添加了空课表测试*\n+ *修正了很蠢的星期七bug*\n\n\n# 效果演示\n![在这里插入图片描述](https://img-blog.csdnimg.cn/20200418162200474.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n![在这里插入图片描述](https://img-blog.csdnimg.cn/20200418162257502.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n![在这里插入图片描述](https://img-blog.csdnimg.cn/20200418162215102.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n\n![在这里插入图片描述](https://img-blog.csdnimg.cn/2020041816232963.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n\n\n# 详情信息说明\n\n## 前言\n之前写过一篇[JAVA使用HttpClient模拟登录正方教务系统，爬取学籍信息和课程表成绩等，超详细登录分析和代码注解](https://blog.csdn.net/XiaoYunKuaiFei/article/details/105438789)的教程，在移植到移动平台时候，发现了如下问题：\n+ 抓取课表偶尔会不完全，出现**全部乱码**的情况\n+ HttpClient相关包**与SDK冲突**，导致移植安卓出现问题\n+ 教务系统偶尔会弹出**验证码**，导致*登陆失败*\n+ 没有现成的**课程表界面**\n\n\n\n### 问题分析\n#### 解决抓取课程乱码\n\u003e当时使用的HttpClient3 已经过时，官方已经更新新版本\n\n造成这个问题的原因是因为当时使用的`HttpClient3`已经过时，官方已经停止维护，因为年过许久部分功能已经不在适应当前的HTTP协议，导致部分传输不完全或者丢包从而造成乱码，解决方法也很简单粗暴，**直接换用`HttpClient5`**，抛弃之前的Post/getmode，换用新的HttpGet/Post，以及结果集；\n**为了避免乱码，这里使用了阿里巴巴的JSON库`fastjson`来解析得到的响应JSON**\n```java\n  response=httpClient.execute(httpPost);\n   HttpEntity kcb=response.getEntity();\n    jsonObject = JSON.parseObject(EntityUtils.toString(kcb,\"UTF-8\"));\n \tJSONArray timeTable = JSON.parseArray(jsonObject.getString(\"kbList\"));\n        for (Iterator iterator = timeTable.iterator(); iterator.hasNext();){\n         JSONObject lesson = (JSONObject) iterator.next();\n         Course course=new Course();\n         course.setId(usernam);\n         course.setName(lesson.getString(\"kcmc\"));\n            ……\n        //这里都是一样的，太长了，为优化阅读，此次省略，参见下文\n         courses.add(course);\n        }\n```\n*注意：这里的Course为一个课程对象，方便对课程进行操作，具体结构会在下文提到*\n#### 解决HttpClient与SDK冲突\n\n\u003eHttpClient相关包**与SDK冲突**，导致移植安卓出现问题\n\n造成这个问题的原因是由于谷歌抛弃了阿帕奇的Http架构，移除了SDK，并且和现有SDK包内方法重名，这个问题困扰了很长时间，也在CSDN和其他等平台查阅了很多相关资料，均无从解决，后来在阿帕奇的官方资料上找到了一篇关于这个的解决方案，很简单，**直接在`Gradle`中添加依赖即可**\n```\ndependencies {\n    api 'com.github.ok2c.hc5.android:httpclient-android:0.1.0'\n}\n\n```\n具体可以参考我的另一篇文章[在安卓9.0以上版本使用HttpClient](https://blog.csdn.net/XiaoYunKuaiFei/article/details/105543103)\n\n#### 解决教务系统验证码\n\u003e教务系统偶尔会弹出**验证码**，导致*登陆失败*\n\n解决这个问题，先手动模拟登录出现验证码的情况试一试\n![验证码出现](https://img-blog.csdnimg.cn/20200418144512714.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n先用控制台查看验证码相关的HTML代码\n![HTML代码](https://img-blog.csdnimg.cn/20200418144610164.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n可以看到其指向了一个页面`captcha.html?ts=557`，在前面加上登录页面的前缀得到以下这个网址`https://auth.wtu.edu.cn/authserver/captcha.html?ts=557`\n经过反复刷新和测试，发现后面的`ts=`偶尔变化偶尔不变，猜想这个ts可能是一个无关的时间参数，直接去掉ts访问网页试一试![验证码](https://img-blog.csdnimg.cn/20200418144911226.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n果然，验证码出现了，查看网络请求：\n![在这里插入图片描述](https://img-blog.csdnimg.cn/20200418145035976.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n![在这里插入图片描述](https://img-blog.csdnimg.cn/2020041814511431.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n**一个Get带上请求的Cookie，没有其他参数**\n虽然无法得知验证码为什么会出现，但是已经得到了如何获取验证码，那么直接简单粗暴的每一次请求都获取验证码，并且输入，这样就可以避免偶尔出现验证码时导致登录失败；\n##### 使用`HttpClient5`抓取验证码：\n同之前一样，直接用GET方法带上之前的Cookie发送请求，拿到响应内容\n```java\nString chakUrl = \"https://auth.wtu.edu.cn/authserver/captcha.html\";\n        httpGet = new HttpGet(chakUrl);\n        try {\n            response = httpClient.execute(httpGet);\n        } catch (IOException e) {\n            return toEffexecute(\"解析验证码错误\");\n        }\n        HttpEntity entityCheck = response.getEntity();\n```\n响应内容是一张图片，那就直接转换为字符流显示出来\n\n```java\n        InputStream inputStream = null;//获取字符流\n        try {\n            inputStream = entityCheck.getContent();\n            checkIMG =BitmapFactory.decodeStream(inputStream);//读取图像数据\n            inputStream.close();\n        } catch (IOException e) {\n            return toEffexecute(\"加载验证码失败\");\n        }\n        checkIMG.setImageBitmap(helper.getCheckIMG());\n```\n\n*以上是在Android上使用字符流保存图片然后直接显示在屏幕上*\n移动平台上可以采用以下方法将字符流转换为字节流写入到文件直接保存到本地：\n\n```java\n InputStream inputStream=entityCheck.getContent();//获取字符流\n        OutputStream os = new FileOutputStream(\"C:\\\\chakOK.jpg\");//写入流\n        byte[] b = new byte[1024];//缓冲区\n        int temp = 0;//长度\n        while ((temp = inputStream.read(b)) != -1) {\n            os.write(b, 0, temp);\n        }\n        os.close();\n        inputStream.close();\n```\n##### 发送验证码结果\n上述是抓取验证码，那拿到了验证码后如何将验证码的结果告诉教务系统呢？继续手动登录模拟：\n![在这里插入图片描述](https://img-blog.csdnimg.cn/20200418150230709.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n对比之前的模拟登录，可以看到参数中多了一个`captchaResponse\t`字段，很明显这就验证码，那么这就与之前模拟登录一模一样了，这是多了个验证码字段，将它填入即可\n\n```java\n String loURI = \"https://auth.wtu.edu.cn/authserver/login;jsessionid=\" + JSESSION + \"?service=http%3A%2F%2Fjwglxt.wtu.edu.cn%2Fsso%2Fjziotlogin\";\n        ;\n        httpPost = new HttpPost(loURI);\n        //请求头\n        httpPost.setHeader(\"Accept\", \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\");\n        httpPost.setHeader(\"Accept-Encoding\", \"gzip, deflate, br\");\n        httpPost.setHeader(\"Accept-Language\", \"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\");\n        httpPost.setHeader(\"Connection\", \"keep-alive\");\n        httpPost.setHeader(\"Content-Type\", \"application/x-www-form-urlencoded\");\n        httpPost.setHeader(\"Cookie\", \"route=\" + ROUTE + \"; JSESSIONID_auth=\" + JSESSION);\n        httpPost.setHeader(\"Host\", \"auth.wtu.edu.cn\");\n        httpPost.setHeader(\"Origin\", \"https://auth.wtu.edu.cn\");\n        httpPost.setHeader(\"Referer\", url);\n        httpPost.setHeader(\"User-Agent\", \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0\");\n        //参数\n        List\u003cNameValuePair\u003e pairs = new ArrayList\u003c\u003e();//创建List集合，封装表单请求参数\n        pairs.add(new BasicNameValuePair(\"username\", usernam));\n        pairs.add(new BasicNameValuePair(\"password\", password));\n        pairs.add(new BasicNameValuePair(\"captchaResponse\", cheack));\n        pairs.add(new BasicNameValuePair(\"lt\", lt));\n        pairs.add(new BasicNameValuePair(\"dllt\", \"userNamePasswordLogin\"));\n        pairs.add(new BasicNameValuePair(\"execution\", \"e1s1\"));\n        pairs.add(new BasicNameValuePair(\"_eventId\", \"submit\"));\n        pairs.add(new BasicNameValuePair(\"rmShown\", \"1\"));\n        //创建表单的Entity对象,将表单存入其中用UTF-8编码\n        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs, Charset.forName(\"UTF-8\"));\n        //写入参数\n        httpPost.setEntity(formEntity);\n\n        //执行\n        try {\n            response = httpClient.execute(httpPost);\n        } catch (IOException e) {\n            return toEffexecute(\"发送请求失败\");\n        }\n```\n至此，关于验证码的问题就解决了，又可以链接上教务系统了！\n\n#### 解决没有课程表界面\n\u003e没有现成的**课程表界面**\n\n遇到了这个问题，没有别的解决办法，就直接造一个吧！简单梳理多款相关软件的布局，并且自己就是学生，按照自己的需求，将界面进行如下刨析：\n![在这里插入图片描述](https://img-blog.csdnimg.cn/20200418151541290.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpYW9ZdW5LdWFpRmVp,size_16,color_FFFFFF,t_70)\n布局如上，一个很简单的三段布局，但是有一个很致命的问题出现了，课程不一定是连续的，并且可能会有课程重合*例如部分有些课程时间一样，但是起止周不相同*，这个时候使用典型的线性布局肯定不可以，在思考后，发现了帧布局`FrameLayout`这么个玩意儿，我的解决方法如下：\n\u003e1.整个全局为一个`LinearLayout`，最上方的时间处用一个横向的`LinearLayout`平均8等分，用于之后的天数定位\n\u003e\n\u003e2.下方为一个`ScrollView`，包裹一个`LinearLayout`和一个`FrameLayout`，按照之前天数的划分分别占1份和7份，在`LinearLayout`添加若干高度相同的子块用于表示节次；\n\u003e3.之后直接在运行时动态新建`view`宽度和之前划分的每一份相同，高度为`节次数*子块高度`，根据课程的时间设置`Margins`：，\n\u003e`上边距=子块高度*（上课节-1）`\n\u003e `左边距=份数大小*（星期数-1）`\n\n具体布局代码参考 `app/layout/course_activity.xml`\n\n自此，刚刚遇到的4个问题都已经解决，现在可以开始构建最终的APP了；\n\n## 最终代码\n#### 定义Course对象\n为了存储课程信息方便后续使用，定义一个`Course`对象；\n\n```java\n  \tprivate String id=\"\";//课程唯一编号\n    private String name=\"\";//课程名\n    private String campus=\"\";//校区\n    private String room=\"\";//教室\n    private String week=\"\";//星期\n    private int tMin=1;//开始节\n    private int tMax=2;//结束节\n    private int wMin=1;//开始周\n    private int wMax=2;//结束周\n    private String teacher =\"\";//老师\n    private String job=\"\";//职务\n    private String test=\"\";//考试方法\n```\n其中第一`getCV()`方法用于将对象转换为`ContentValues`键值对，方便写数据库使用：\n\n```java\npublic ContentValues getCV(){\n        ContentValues values=new ContentValues();\n        values.put(\"id\",id);\n        values.put(\"name\",name);\n        values.put(\"campus\",campus);\n        values.put(\"room\",room);\n        values.put(\"week\",week);\n        values.put(\"tMin\",tMin);\n        values.put(\"tMax\",tMax);\n        values.put(\"wMin\",wMin);\n        values.put(\"wMax\",wMax);\n        values.put(\"teacher\", teacher);\n        values.put(\"job\",job);\n        values.put(\"test\",test);\n        return values;\n    }\n```\n同理定义一个静态`getCourses(Cursor cursor)`方法，直接将`Cursor`转换为对象数组，方便读取数据库显示课表使用：\n\n```java\n public static List\u003cCourse\u003e getCourses(Cursor cursor){\n        List\u003cCourse\u003e courses=new ArrayList\u003c\u003e();\n        while (cursor.moveToNext()){\n            Course course=new Course();\n            course.setId(cursor.getString(0));\n            course.setName(cursor.getString(2));\n            course.setCampus(cursor.getString(3));\n            course.setRoom(cursor.getString(4));\n            course.setWeek(cursor.getString(5));\n            course.settMin(cursor.getInt(6));\n            course.settMax(cursor.getInt(7));\n            course.setwMin(cursor.getInt(8));\n            course.setwMax(cursor.getInt(9));\n            course.setTeacher(cursor.getString(10));\n            course.setJob(cursor.getString(11));\n            course.setTest(cursor.getString(12));\n            courses.add(course);\n        }\n        return courses;\n    }\n```\n#### 建立SQLite数据表\n避免每次启动都有登录教务系统，直接将课程信息保存到本地数据库，登录一次后每次就可以直接跳转到课表\n```java\npublic class DBhelp extends SQLiteOpenHelper {\n    public static final String T_COURSE=\"t_course\";\n    public class ContactTable implements BaseColumns {\n        public static final  String ID=\"id\";\n        public static final  String NAME=\"name\";//课程名\n        public static final  String CAMPUS=\"campus\";//校区\n        public static final  String ROOM=\"room\";//教室\n        public static final  String WEEK=\"week\";//星期\n        public static final  String TMIN=\"tMin\";//开始节\n        public static final  String TMAX=\"tMax\";//下课节\n        public static final  String WMIN=\"wMin\";//开始周\n        public static final  String WMAX=\"wMax\";//结束周\n        public static final  String TEACHER=\"teacher\";//老师\n        public static final  String JOB=\"job\";//职务\n        public static final  String TEST=\"test\";//考试方法\n    }\n\n    public DBhelp(@Nullable Context context) {\n        super(context, \"course.db\", null, 1);\n    }\n\n    @Override\n    public void onCreate(SQLiteDatabase db) {\n        String sql=\"Create table \"+T_COURSE+\" (_id integer  PRIMARY KEY AUTOINCREMENT,\"\n                +ContactTable.ID+\" text,\"\n                +ContactTable.NAME+\" text,\"\n                +ContactTable.CAMPUS+\" text,\"\n                +ContactTable.ROOM+\" text,\"\n                +ContactTable.WEEK+\" text,\"\n                +ContactTable.TMIN+\" integer,\"\n                +ContactTable.TMAX+\" integer,\"\n                +ContactTable.WMIN+\" integer,\"\n                +ContactTable.WMAX+\" integer,\"\n                +ContactTable.TEACHER +\" text,\"\n                +ContactTable.JOB+\" text,\"\n                +ContactTable.TEST+\" text)\";\n        db.execSQL(sql);\n    }\n\n    @Override\n    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n\n    }\n}\n```\n#### 封装模拟登录工具类\n为了方便调用和日后扩展，我将所有与模拟登录相关的方法全部封装于一个`JWhelper`类中，供之后直接调用，在这里使用的是`HttpClient5`与上一篇文章的实现大致相仿，只不过实现方法的细节不同，因此就****不再在此次进行原理分析****，具体原理可以参考[我的上一篇文章](https://blog.csdn.net/XiaoYunKuaiFei/article/details/105438789)，\n\n参看`app\\cn.edu.wtu.kcb.util.JWhelper`\n\n\n说明：\n+ `isCheckIMG()`用于用Get方法获取验证码，并解析，之后可以调用`getCheckIMG()`获取到解析的图片，并显示在界面上\n+ `login(String id, String pass, String code)`方法在其中判断返回的状态码是否为302判断登录是否成功并使用返回一个`boolean`，为此这个方法还可以进行一些扩展操作例如**判断绑定学号**等\n+ `getURI()`方法获取最终的Cookie和URI，并存储在内存中，同样也可以扩展，列如**获取学生信息、获取成绩、抢课**等操作\n+ `isCourse(String year,String semester)`方法用于获取课程表，并提供JSON解析，结果用` getCourses()`方法返回一个课程数组，同样结合`getURI()`,按照本方法的范例可以实现**获取学生信息、获取成绩、抢课、一键教学评价**等更多操作；\n\n*写入课程到数据库的方法就不在这里赘述了*\n****\n#### 显示课程表\n上文已经定义了XML布局，这里直接用代码来实现刚刚留下的空白\n##### 1、获取参数\n首先获取屏幕宽度，并且平均为8，用于之后定位课程位置与大小\n```java\n   int d = getWindowManager().getDefaultDisplay().getWidth() / 8;\n```\n##### 2、获取当前周数\n利用`Calendar`取得当前的日期和周数，显示到布局上的星期上，并且将当天的背景显示颜色突出\n*需要注意的是这里使用的`SharedPreferences`来判断和保存用户是否设置过周数*\n```java\nTextView[] weekTexts = {weekOne, weekTwo, weekSan, weekSi, weekWU, weekLiu, weekDAY};\n        //获取当前周数\n        int weeks = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);\n        //打开SP\n        SharedPreferences sp = getSharedPreferences(KCB, MODE_PRIVATE);\n        //判断是否设置周数\n        if (sp.getBoolean(\"flag\", false)) {\n            //读取设置时候的,如果没有就是weeks,默认为1\n            int theWeek = sp.getInt(\"week\", 1);\n            //读取设置时候的日期\n            int y = sp.getInt(\"year\", 2020);\n            int m = sp.getInt(\"math\", 3);\n            int day = sp.getInt(\"day\", 3);\n            //读取设置时候的日期\n            Calendar calendar = new GregorianCalendar(y, m, day);//日期对象\n            //获取设置那天的周数\n            int theWeekofY = calendar.get(Calendar.WEEK_OF_YEAR);\n            Log.d(\"TAGG\", \"iniView: \" + theWeekofY);\n            //得到当前周数\n            weeks = (weeks - theWeekofY) + theWeek;\n             }\n        weekNums.setText(weeks + \"周\");\n        //获取当前周几，转换为中国周（周一为第一天）\n        int weekday = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;\n        //设置日期\n        for (int i = 0; i \u003c 7; i++)\n        weekTexts[i].append(\"\\n\" + getDate(i - weekday));\n         TextView textNow = new TextView(this);\n        LinearLayout.LayoutParams paramNow = new LinearLayout.LayoutParams(d, dip2px(14 * 65));\n        textNow.setBackgroundColor(0x40E91E63);\n        paramNow.setMargins((weekday - 1) * d, 0, 0, 0);\n        textNow.setLayoutParams(paramNow);\n        PFF.addView(textNow);\n```\n##### 3、读取数据库中的课程信息\n这里用到了之前上文定义的`getCourses(cursor)` 方法，直接返回了数组，两行代码就完成了，是不是非常方便\n```java\n Cursor cursor = getContentResolver().query(CourseProvider.URI_COURSE, null, null, null, null);\n        List\u003cCourse\u003e courses = Course.getCourses(cursor);\n```\n\n##### 4、判断是否非本周并给课程表上色\n这里用到了一个变量`i`，由于每个课程最多会有4个相邻的课程，为了****避免显示到一起颜色相同不便分辨****我设置了5种不同的颜色，这样可以尽量避免相邻的两个课程颜色相同，并且比较**本周与起止周数**的大小判断本周是否有课，如果没课直接涂灰，并加上`[非本周]`提示，拼接课程信息用于显示；\n```java\nint i=0\nfor (final Course course : courses) {\n            //新建文本\n            final TextView textView = new TextView(this);\n            //设置颜色\n            i++;\n            switch (i % 5) {\n                case 0:\n                    textView.setBackgroundColor(0xffCDDC39);\n                    break;\n                case 1:\n                    textView.setBackgroundColor(0xff22ccff);\n                    break;\n                case 2:\n                    textView.setBackgroundColor(0xff22ffcc);\n                    break;\n                case 3:\n                    textView.setBackgroundColor(0xff00BCD4);\n                    break;\n                case 4:\n                    textView.setBackgroundColor(0xffF44336);\n                    break;\n            }\n            //显示字符串\n            String str = \"\";\n\n            //获取当前课的周期,转换为纯数字和-，按-分隔\n           int MIN=course.getwMin();\n           int MAX=course.getwMax();\n            if (MIN \u003e weeks || weeks \u003e MAX) {\n                str += \"[非本周]\";\n                //设置灰色\n                textView.setBackgroundColor(0xcccccccc);\n            }\n            str += course.getName();\n            str += \"@\" + course.getRoom();\n            str += \"#\" + course.getTeacher() + course.getJob();\n            str += \"|\" + course.getTest();\n            textView.setText(str);\n```\n##### 5、定位并显示课程\n如上文所诉的原理，工具起止节和星期，分别设置高度，和左、上边距，从而达到定位课程的效果，最后添加到`FrameLayout`，显示课表；\n```java\n MIN=course.gettMin();\n            MAX=course.gettMax();\n            int height =MAX-MIN+1;\n            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(d, dip2px(height* 65));\n            //设置位置\n            int week = 6;\n            switch (course.getWeek()) {\n                case \"星期一\":\n                    week = 0;\n                    break;\n                case \"星期二\":\n                    week = 1;\n                    break;\n                case \"星期三\":\n                    week = 2;\n                    break;\n                case \"星期四\":\n                    week = 3;\n                    break;\n                case \"星期五\":\n                    week = 4;\n                    break;\n                case \"星期六\":\n                    week = 5;\n                    break;\n                case \"星期七\":\n                    week = 6;\n                    break;\n                default:\n                    break;\n            }\n            //左边-星期，  上边-节\n            params.setMargins(week * d, dip2px((MIN - 1) * 65), 0, 0);\n            textView.setLayoutParams(params);\n            textView.setTextSize(12);\n            textView.setTextColor(Color.WHITE);\n            //结尾显示省略号\n            textView.setEllipsize(TextUtils.TruncateAt.END);\n            //添加到容器\n            PFF.addView(textView);\n```\n##### 6、内部方法：dp转px\n手机屏幕和方法内设置的数值单位是px，XML中的布局单位是DP，为了保持一致，查阅资料后发现了一个方法，直接将dp 转换为px：\n\n```java\n  public int dip2px(float dpValue) {\n        Context context = this;\n        final float scale = context.getResources().getDisplayMetrics().density;\n        return (int) (dpValue * scale + 0.5f);\n    }\n```\n至此这个APP搭建完毕*省略了登录页面，这个实现方法已经在上文`JWhelper（）`中实现，具体界面可以自己实现,本文实在是太长了，不在赘述*\n\n## 后记\n以上是安卓APP抓取教务系统课表等信息并显示的主要部分分析和代码实现，**本项目的源码我已经开源放到了GitHub上**，大家可以在GitHub上下载源码进行扩展创作：\n\u003e[ **点击跳转GitHub Android-JAVA-ZFeducation-system**](https://github.com/lengpucheng/Android-JAVA-ZFeducation-system)\n\n目前已经实现了如下功能：\n+ 1、通过教务系统`判断学号密码`\n+ 2、模拟登录`抓取课程信息`\n+ 3、根据课程信息`显示课程表`\n+ 4、`动态判断是否本周`\n+ 5、自定义`添加`、`编辑`、`删除`课程\n+ 6、自动`判断课程导入`\n+ 7、`高亮显示当天`\n并且预留了以下实现接口：\n(*主要是一个人，一次性写完时间太长*）\n+ 1、通过教务系统`读取学籍信息`、`抓取照片`\n+ 2、`抓取成绩`\n+ 3、`空教室查询`\n+ 3、`POST选课`\n+ 4、`教学评价`\n+ 5、`新闻和通知获取`\n因此，欢迎大家拷贝下载，一起完善该项目！\n\n**转载请注明出处**\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flengpucheng%2Fandroid-java-zfeducation-system","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flengpucheng%2Fandroid-java-zfeducation-system","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flengpucheng%2Fandroid-java-zfeducation-system/lists"}