Http Communication for Android-1. First knowledge of Http protocol and first knowledge of android

Source: Internet
Author: User

Http Communication for Android-1. First knowledge of Http protocol and first knowledge of android
Http Communication for Android-1. First understanding of Http Protocol Introduction:

Today is children's day. Let's say Happy holidays to all over-age children ~ (Small □small), pig also symbolically sent a red envelope to the children in the group... Well, the busy May s have finally passed. I have finished completing my work, writing my papers, taking graduation photos, replying, resigning, and all kinds of things, pig now has another job in a new company, and the third company, you will surely feel that pig is two-minded. After all, it has changed to so many companies after almost graduation, then you may ask me, "When did you jump to the fourth company, pig?", haha, it should be okay. What should I do? In fact, I am still very specific. After all, I just graduated, I don't need to support my family, support the elderly, and I don't have a girlfriend, haha ~ Therefore, when you are young, you can choose the soil suitable for your own growth, learn more skills, and pay nothing. To be honest, learning something is the key. When you really have the skills, at that time, I could talk about my salary. For example, my company's Daniel (but I have already switched to meizu this week). For the first time, I felt that bgex was really high and I feel inferior to myself, people write components, push, various code encapsulation, various design modes, and server, test, IOS... In a novel, when a beginner apprentice saw a master at the master level, he thought that I could really take over his project after a few days of inferiority? Or understand his code? Or can I write such code? In those days, the whole person was very impetuous, and all kinds of negative emotions could not even be seen in the Code. Later, I saw a design-oriented TED speech titled: "How to become a good designer: Luo zixiong", although this does not mean programming, but in fact many things are the same, divided into two stages: Entry & quasi-professional level, "You want to be a designer, but you want to be a good designer. However, if you don't have a life, you will be excellent. If you don't have a life, you will have a uidesign, graphic design, and web design .", How much experience do we need from entry level to quasi-professional level ?" In his book "heterogeneous", Gladwell pointed out: "The talents in the eyes of people are not extraordinary, but they have made continuous efforts, 10 thousand hours of tempering is essential for anyone to change from ordinary to extraordinary." 10 thousand hours, that is, you work 8 hours a day, 5 days a week, you need 5 years. You don't need a genius, you don't need an IQ, you don't need three heads and six arms, you don't need a long horn, you just need continuous, persistent efforts, there is a correct method, you can be in the design field, one Professional is the only one." After reading this, the whole person is very cheerful and calm down. Right, how long have people been doing it? How long have I been doing it? I have been working as an intern for five years. I am just a new intern. I don't have a lot of things. I don't have to worry about it. I plan every day and keep one step at a time. I have been stuck for five years, or no, I can become a quasi-professional master, right? So why should I feel inferior ~ Well, let's talk about things in chicken soup. drinking too much wine is useless. The video is still good. If you are interested, you can check it out. Okay, starting from today, pig will summarize some related things while learning about the company's projects and share them with you. The first chapter describes Http Communication in Android, first, we will describe some Http-related concepts, and then talk about two methods for sending Http requests on Android: HttpUrlConnection and HttpClient, and then encapsulate our HTTP requests using the fit framework, at last, we use RxJava to implement responsive programming and Asynchronous Network request calling. Of course, pig is also learning. If there is anything wrong with it, I would like to express my gratitude ~

Body: 1. What is Http? ① Glossary:
  • Hypertext transfer protocol (hypertext transfer protocol), a TCP/IP protocolApplication Layer Protocol, Used to defineWEB browser and WEB ServerThe process of data exchange between them. After the client connects to the web server, if you want to obtain a web Resource in the web server, you must comply with a certain communication format. the HTTP protocol is used to define the communication format between the client and the web server.
② The differences between Http 1.0 and Http 1.1:
  • 1.0 protocol. After the client establishes a connection with the web server, only one web resource can be obtained!
  • 1.1 protocol: allows the client to establish a connection with the web server and then obtain multiple web resources from one connection!

PS: most of them are now using the Http 1.1 protocol ~

③ Http request workflow:

Here we will first introduce two terms: SYN and ACK.

  • SYN (synchronous): handshake signal used to establish a connection between TCP/IP
  • ACK (Acknowledgement): confirm the character and confirm that the data sent has been accepted

Next, let's talk about the three-way handshake of TCP/IP:

If you think it is complicated, you may have an impression. The three handshakes are as follows:

Http operation process:

Okay, No pic you say a jb. If there is a picture of the truth, we will use HttpWatch to capture the package (of course, you can also use Chrome f12 directly ), the website tested here is the educational administration system of pig school. after entering the account and password, the system sends a login request and the relevant information header. As for the relevant information header, we will explain it in detail next section ~ :

HTTP request content:

HTTP Response content:

④ Http request methods:

We can see that the above method of sending an http request is POST, and it and GET are used much in our usual development. Next we will list all the request methods:

  • Get: Request to obtain the resource identified by Request-URI
  • POST: append new data to the resource identified by Request-URI
  • HEAD Request to obtain the response information header of the resource identified by Request-URI
  • PUT: The Request server stores a resource and uses Request-URI as its identifier.
  • DELETE: The Request server deletes the resource identified by Request-URI.
  • TRACE: the information of the request received by the request server. It is mainly used for testing or diagnosis.
  • CONNECT: retain future use
  • OPTIONS: query the server performance or query resource-related OPTIONS.

Well, except for GET and POST, I have never used it. So here we only distinguish GET from POST:

  • GET: After the requested URL address? The data is carried to the server. Multiple Data are separated by &, but the data capacity cannot exceed 2 K. For example: http: // xxx? Username =... & Pawd =... This is GET.
  • POST: this can send data to the server in the request's entity content. There is no limit on the quantity of data transmitted.
  • In addition, both of these tools send data, but the sending mechanism is different. Don't trust what we say on the Internet: "GET server data, POST send data to the server "!!
    In addition, the GET security is very low, and the Post security is high, but the execution efficiency is better than the Post method. Generally, we use GET when querying, and POST when adding, deleting, and modifying data !!
⑤ Http status code collection:
⑥ Features of Http

All right, you can just copy and paste these items ~

7. OSI Layer-7 protocol & TCP layer-4 protocol;

Well, we can understand this. If you need it later, we can further understand that our HTTP requests come from the Seventh Layer: application layer,
Everyone knows. In addition, it is better to write down these seven layers. By the way, one to three layers are used to create physical connections between two network devices,
Layer 4 to Layer 7 is mainly responsible for interoperability. It is good to be familiar with it. You may ask for an interview, right? haha ~

OSI Layer-7 protocol diagram & TCP layer-4 model diagram:

In addition, when I checked the relevant information, I found a higher summary map of B cells. If you are interested, you can study it slowly:

Paste the original article link. If you want to answer the question, you can see:

Detailed description of OSI Layer-7 Model

Summary:

Well, because the time is too short, after all, it can only be written when I get off work. It may be a bit messy. I hope I can understand it. If I have time, I will try again, for some of the content, refer to the content of the "Android HTTP Communication" video of MOOC. If you are interested, you can go to MOOC. Well, let's summarize the following content:

This section provides some conceptual things. For more information, see. In the next section, we will study different response headers. Our server will control browser behaviors by setting different response headers, for example, page orientation, timed refresh, and file download prompt. Please wait ~ Okay, take a bath and go to bed. Continue to work tomorrow ~

PS: My roommate passed by and asked me what I was deducting for one night? Do I write code? I want to write blogs. Then, he asked me if I could make money? I said no. Then he said, there was an egg... I don't know how to explain it to him. Everyone's views and values are different. I like to write blogs. Even though I am not a great guy, I have nothing to do with B, it may be basic things. Even so, I am still willing to bury myself in the code for one night after I finish my work. What a thousand miles, sustained, and persistent efforts will not be accumulated, coupled with the correct method, pig also became a great cow on that day. In addition, it is easy to write out the method to help later users. Why not? Right! I would like to share with all the siege lions ~ Fuel

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.