Android HTTP protocol access network instance (3 kinds)
The previous analysis on Android HTTP protocol accessing the network needs to be reviewed recently, so I sent it to the essay by the way
HTTP connection in Android is mainly used to obtain network data. At present, there are three methods:
httpconnection
Because the network connection is a time-consuming operation, it cannot be operated in the UI thread. Generally, the data obtained in the child thread is obtained through the handler
How to call the Http method class in UI thread (long ago pondering over a long time):
The returned types are all strings, which are written directly in a class for later convenience
If the code is not complex, I won't repeat it. Let's talk about other points:
thread:
I often hear people ask what's the difference between sleep () and wait (). The sleep () method comes from the thread class, while the wait () method comes from the object class; Secondly, when the sleep () method is called, it does not transfer system resources. Wait () gives up system resources, and other threads can occupy CPU; Finally, sleep (miles econds) needs to specify a sleep time, and it will wake up automatically when the time comes.
HTTP (Hypertext Transfer Protocol), HTTPS (Hypertext Transfer Protocol Secure version)
About TCP / IP and UPD:
TCP / IP is more stable than UDP, but it is slower because it has three handshakes
About the third Handshake:
There are many explanations on the Internet. My own description is that the host asks the server first. Can I send data? The server answers, yes. The host is saying, I send it. The specific content will be understood in detail later, and the figure above will be used first
OKHTTP
The description in the book is that an excellent network communication library can be used to replace HTTPCONNECTION. I'll use it if you say it's excellent.
Add dependency:
use
Access the network via URL ():
After that, we call the newCall () method to create the call object and call the excute () method to send the request and get the server data:
Let's talk about the above two methods. They only talk about get and no post (submit data). The reason is that they are lazy. We'll talk about it later
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.