-
The client sends an HTTP request to the server, and the server returns some data to the client after receiving the request, which is then parsed and processed by the client.
You can use httpurlconnection (officially recommended) to send http requests.
layout files:active classes:
Because UI operations are not allowed in child threads in Android, we use the RunonUIWet method to switch to the main thread and then update the UI elements.
Finally, remember to declare network permissions
OKhttp is an open-source project that handles web requests and is currently the hottest lightweight framework for Android, with contributions from mobile payments company Square (which also contributed Picasso). Want to replace httpurlconnection and Apache httpclient.
Introduce the OKhttp library dependencies first:
Then click Sync Now in the top right corner of Android Studio to actually load the library.
Modify Activity Class:
There are a number of other methods that can be preceded by the build() method to enrich the request object.
If it's a POST request, you'll need to build a requestbody object like this
Modify Activity Class:
Note:new thread(..After that, you need to run start() to start the thread.
Run: As you can see, OKhttp is more powerful than HTTPURLconnection: the same **, OKhttp is able to return the response data correctly oh o( o haha
-
It depends on the needs of the actual application and the amount of data.
HTTP defines different ways to interact with the server, the most basic of which are get and post.
In fact, get works for most requests, and reserved POST is only used to update the site. According to the HTTP specification, GET is used for information fetching and should be secure and idempotent. What we mean by secure means that the operation is used to obtain the information and not to modify it.
In other words, get requests should generally not result in ***. Idempotent means that multiple requests to the same URL should return the same result. The full definition is not as strict as it seems.
Fundamentally, the goal is that when a user opens a link, it can be confident that the resource has not changed from its own point of view. For example, the front page of a news site is constantly updated. Although the second request returns a different batch of news, the operation is still considered safe and idempotent because it always returns the current news.
Vice versa. POST requests are not so easy. POST indicates a request that may alter the resources on the server. In the case of a news site, a reader's comment on an article should be made through a post request, because the site is different after the comment is submitted (for example, a note appears below the article);
When the form is submitted, if the method is not specified, it will be a get request by default, and the data submitted in the form will be appended to the url. Separate from URLs. Alphanumeric characters are sent as-is, but spaces are converted to "+" signs, and other symbols are converted to %xx, where xx is the ASCII (or ISO latin-1) value of the symbol in hexadecimal.
The data submitted by the GET request is placed in the HTTP request protocol header, while the data submitted by POST is placed in the entity data.
The data submitted by the get method can only have a maximum of 1024 bytes, while the POST does not have this limit.
-
The difference between a GET request and a POST request.
GET is a request to the server for data, while POST is a request to submit data to the server.
Get is to get the information, not to modify the information, and like the database query function, the data will not be modified.
The parameters of the GET request will be passed after the URL, and the requested data will be appended to the URL. Split the url and the transmitted data, the parameters are connected with &, the xx in xx is the ASCII of the symbol in 16 base, if the data is English alphanumeric alphanumeric and sent as is, if it is a space, convert to +, if it is Chinese Other characters, then directly encrypt the string with base64.
There is a size limit on the data transmitted by GET, because GET submits data through URLs, so the amount of data that GET can submit is directly related to the length of URLs, and different browsers have different limits on the length of URLs.
The data requested by GET will be cached by the browser, and the username and password will appear in plaintext on the URL, so that others can check the historical browsing history, and the data is not very secure. On the server side, it is used to obtain the data submitted in the GET mode.
The POST request is sent to the web server as the actual content of the HTTP message, and the data is submitted in the HTML header, and the POST does not limit the data submitted. Post is more secure than get, when the data is Chinese or non-sensitive data, then use get, because with get, the parameters will be displayed in the address, for sensitive data and data that is not Chinese characters, then use post
POST indicates a request to modify resources on a server, and data submitted in POST mode can only be used to obtain data on the server.
-
1.get is to get data from the server, and POST is to send data to the server.
2.GET adds the parameter data queue to the URL referred to by the action attribute of the submission form, and the value corresponds to each field in the form one-to-one, which can be seen in the URL. POST is an HTTP POST mechanism that sends each field in the form to the URL address referred to by the action attribute along with its content in the HTML header.
The process is not visible to the user.
3.For the get mode, the server uses to get the value of the variable, and for the post mode, the server uses to get the submitted data.
4.The amount of data transmitted by get is small, and cannot be greater than the amount of data transferred, and is generally unrestricted by default. But theoretically, the maximum amount is 80kb in IIS4 and 100kb in IIS5.
5.GET security is very low, POST security is high. However, the execution efficiency is better than that of the POST method.
Suggestions: 1. The security of the get method is worse than that of the POST method, and if it contains confidential information, it is recommended to use the POST data submission method;
2. When doing data query, it is recommended to use the get method; When adding, modifying, or deleting data, it is recommended to use the POST method.
Personally, I think the security of POST is much higher than GET. >>>More
Brotherly love, you have to believe in her, this is the basic truth, you don't understand? >>>More
Method steps.
The first step is to open the Environment Variable Configuration window. Right-click on the computer, Properties - Advanced System Settings - Environment Variables. >>>More
A few people upstairs are all right, you can take a look at it. I will tell you from my experience of studying chemistry for ten years: chemistry is definitely not difficult to learn! >>>More
You should read the source code and follow the inheritance relationship through Android Studio, which is faster than reading a book. >>>More