Arch arch Lite development (1): Call the Three-wing API for login, Course table acquisition, and meal card information ......, Lite

Source: Internet
Author: User

Arch arch Lite development (1): Call the Three-wing API for login, Course table acquisition, and meal card information ......, Lite

Gongarch Lite is my first work on UWP. I am also happy to share what I learned during development. Therefore, writing this series of articles is also a kind of accumulation, hope to help ~

(1) starting from the official documentation

First, we will analyze the API documentation provided by the three wings:

We can see that this API contains URL and parameters. First, let's take a look at how to pass these five parameters and get data.

Format: URL + '? '+' Parameter Name: 1' + '=' + 'parameter 1' + '&' + 'Parameter Name: 2' +' = '+ 'parameter 2' + ......

We can see that the parameters to be passed are filled in the above format respectively, and they are connected. Here, we will remember to use 'http' rather than 'http' (because we have used SSL encryption, the three-wing official document should have updated ......) Then, open the URL with the correct parameter in the browser and check the returned information:

{"Code": 0, "msg": "success", "data": {"1": {"2": [{"course": "engineering Economics ", "location": "xingjiao building C401", "teacher": "Associate Professor Liu xingwang", "week": "9, 10, 11, 12, 13, 14, 15, 16", "week_string ": "9-16 (Weeks)", "section_length": "2", "section_start": "3", "section_end": "4"}], "3 ": [{"course": "Chemical Engineering Principle III", "location": "Yifu Lou-409", "teacher": "Li yongfei Lecturer (university)", "week ": ", 11", "week_string": "1-11 (week)", "section_length": "2", "section_start ": "5", "section_end": "6"}], "4": [{"course": "Environmental Engineering 3", "location": "Yifu Building-402 ", "teacher": "instructor Chen yuehui (university)", "week": ",", "week_string ": "1-12 (Weeks)", "section_length": "2", "section_start": "7", "section_end": "8"}]}, "2 ": {"1": [{"course": "probability theory III", "location": "Computing Center B202", "teacher": "Liu Jinlong", "week ": "1, 2, 3, 4, 5, 6, 7, 8", "week_string": "1-8 (Weeks)", "section_length": "2", "section_start": "1 ", "section_end": "2"}], "2": [{"course": "Environmental Monitoring II", "location": "1st education building-319 ", "teacher": "Associate Professor Wang xingyan", "week": "1, 2, 4, 5, 6, 7, 8", "week_string": "1-8 (Weeks )", "section_length": "2", "section_start": "3", "section_end": "4"}]}, "3": {"1 ": [{"course": "Chemical Engineering Principle III", "location": "Yifu Lou-409", "teacher": "Li yongfei Lecturer (university)", "week ": ", 11", "week_string": "1-11 (week)", "section_length": "2", "section_start ": "1", "section_end": "2"}], "3": [{"course": "4 4 4", "location":" ", "teacher": "Professor Tan Xinli", "week": "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16", "week_string ": "1-16 (Weeks)", "section_length": "2", "section_start": "5", "section_end": "6"}]}, "4 ": {"1": [{"course": "Environmental Protection Equipment Corrosion and Protection", "location": "1st education building-403", "teacher ": "Chen Hongbo Lecturer (university)", "week": "9, 10, 11, 12, 13, 14, 15", "week_string": "9-15 (Weeks)", "section_length ": "2", "section_start": "1", "section_end": "2" },{ "course": "Environmental Protection Equipment CAD technology", "location ": "Computing Center B402", "teacher": "instructor Chen Hongbo (university)", "week": ",", "week_string ": "1-8 (Weeks)", "section_length": "2", "section_start": "1", "section_end": "2"}], "3 ": [{"course": "Environmental Monitoring Ⅱ", "location": "1st education building-319", "teacher": "Associate Professor Wang xingyan", "week ": "1, 2, 3, 4, 5, 6, 7, 8", "week_string": "1-8 (Weeks)", "section_length": "2", "section_start": "5 ", "section_end": "6"}], "4": [{"course": "probability theory III", "location": "Yifu-110", "teacher ": "Liu Jinlong", "week": ",", "week_string": "1-8 (Weeks)", "section_length": "2 ", "section_start": "7", "section_end": "8"}], "5": [{"course": "Chemical Engineering Principle III", "location ": "Yifu Building-409", "teacher": "Li yongfei Lecturer (university)", "week": ",", "week_string ": "1-10 (Weeks)", "section_length": "2", "section_start": "9", "section_end": "10"}]}, "5 ": {"1": [{"course": "CAD technology for environmental protection equipment", "location": "Computing Center B402", "teacher": "instructor Chen Hongbo (university )", "week": ",", "week_string": "1-8 (Weeks)", "section_length": "2", "section_start ": "1", "section_end": "2"}], "2": [{"course": "Environmental Engineering 3", "location": "Yifu Building-402 ", "teacher": "instructor Chen yuehui (university)", "week": ",", "week_string ": "1-12 (Weeks)", "section_length": "2", "section_start": "3", "section_end": "4"}], "3 ": [{"course": "Corrosion and Protection of environmental protection equipment", "location": "1st education building-403", "teacher": "instructor Chen Hongbo (university )", "week": "9, 10, 11, 12, 13, 14, 15", "week_string": "9-15 (Weeks)", "section_length": "2", "section_start ": "5", "section_end": "6"}] }}

If you can see this large string (JSON), congratulations, you can use the API normally, and we can continue.

(2) Use C # To send a GET request and obtain the JSON

We opened the URL in a browser and obtained JSON. Now we need to use C # to implement the same function and get JSON. I use the HttpWebRequest class recommended by Microsoft:

Public async static Task
 
  
GetJson (string ID, string Passwords) {var http = new HttpClient (); string url = String. Format ("https://api.sky31.com/edu-new/course.php? Role = 201213121 & hash = 0267cc37887e59dqwe2312ddfwe1 & sid = {0} & password = {1} & style = 3 & token = HTTP/1.1 ", ID, Passwords ); httpWebRequest request = (HttpWebRequest) HttpWebRequest. create (url); // Create a request example var response = await http. getAsync (String. format (url); // get the response, that is, send the request var result = await response. content. readAsStringAsync (); return result ;}
 

In this way, we can use C # To send a GET request and obtain data. Below, we will process the data we have obtained.

(3) process JSON

And get the data. Below, we will get the JSON for the above, but we certainly cannot present the data to users. How can we deal with it? Using the string method? NO, will be exhausted. Json is a string in a specific format. in C #, there is a special method for processing it: DataContractJsonSerializer. ReadObject (). Let's use it.

Public class CourseManager {public async static Task
 
  
Manager (string ID, string Passwords) {var result = await GetJson (ID, Passwords); // The result stores the obtained JSONusing (var MS = new MemoryStream (Encoding. unicode. getBytes (result) {DataContractJsonSerializer deseralizer = new DataContractJsonSerializer (typeof (RootObject); RootObject model = (RootObject) deseralizer. readObject (MS); // deserialize ReadObjectreturn model;} [DataContract] public class RootObject // corresponds to {"code": 0, "msg": "successful ", "data" :{}{ [DataMember] public int code {get; set;} [DataMember] public string msg {get; set;} [DataMember] public List
  
   
> Data {get; set;} // The data in JSON is nested with two layers, so here we nest List} [DataContract] public class DataObject // corresponds to {"1 ": [{"course": "Corrosion and Protection of environmental protection equipment", "location": "1st education building-403", "teacher": "Chen Hongbo Lecturer (university )",...... {[DataMember] public string course {get; set;} [DataMember] public string location {get; set;} [DataMember] public string teacher {get; set ;} [DataMember] public string week {get; set;} [DataMember] public string week_string {get; set;} [DataMember] public string section_length {get; set ;} [DataMember] public string section_start {get; set;} [DataMember] public string section_end {get; set ;}} [DataMember] public int day {get; set ;}}}
  
 
[DataContract] and [DataMember] are used to mark attributes and pass in the corresponding JSON value.

In this way, through the ReadObject () method, we can store JSON data to the corresponding class for ease of use. However, if we analyze JSON data one by one and then write the corresponding classes by ourselves, this is not only inefficient, but also prone to errors, so we can find the corresponding tool to automatically convert JSON to C # class

(4) Last step: present data

Now we have saved the obtained data to the instance of the corresponding class. The last step is to present the data on the control. You can directly assign values or bind data. Here I pass the value to the CourseModel bound to the control:

CourseManager.RootObject courses = await CourseManager.Manager(IDTextBox.Text, IDPasswordBox.Password);Int n=0;If(courses.code==0){foreach (List
 
   TheDayCourse in classes.data){if (TheDayCourse[n] != null){foreach (var course in TheDayCourse){var addCourse = new CourseModel(){Course = TheDayCourse[n].course,Teacher = TheDayCourse[n].teacher,Location = TheDayCourse[n].location,Day = TheDayCourse[n].day,Week = WeekstringCut(TheDayCourse[n].week_string),Start = int.Parse(TheDayCourse[n].section_start),End = int.Parse(TheDayCourse[n].section_end)};database.AddData(addCourse);n++;}}}
 
In this way, normally the bound control should be able to receive data updates and display data.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.