In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use OkHttp in Android", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use OkHttp in Android" article.
I. characteristics of OkHttp
Android provides us with two ways to interact with HTTP: HttpURLConnection and Apache HTTP Client, although both support HTTPS, stream upload and download, configuration timeout, IPv6 and connection pooling, which is sufficient to meet our various HTTP requests. But using HTTP more efficiently can make your application run faster and save traffic. And the OkHttp library is created for this reason.
OkHttp is an efficient HTTP library:
§support for SPDY (reference layer protocol), sharing the same Socket to handle all requests from the same server
§if the SPDY is not available, the request delay is reduced by connecting the pool
§seamless support for GZIP to reduce data traffic
§caching response data to reduce duplicate network requests
Automatically recovers from many common connection problems. If your server is configured with multiple IP addresses, when the first IP connection fails, OkHttp will automatically try the next IP. OkHttp also handles proxy server issues and SSL handshake failures.
There is no need to rewrite the network code in your program to use OkHttp. OkHttp implements almost the same API as java.net.HttpURLConnection. If you use Apache HttpClient, OkHttp also provides a corresponding okhttp-apache module.
2. How to import OkHttp
1. Used in Android studio
Add to the gradle:
Compile 'com.squareup.okhttp:okhttp:2.7.0'
Because OkHttp depends on okio, you also need to add:
'com.squareup.okio:okio:1.6.0'
2. Used in Eclipse:
Just download the appropriate Jar package and put it into the project to use.
Download address:
Http://square.github.io/okhttp/#download
Third, how to use it
1. To create a request object, set parameters for the request: use the inner Builder class of the Request class. It is very similar to the way we used to create dialogs and notices in our school. At least one url parameter needs to be set.
Request request = new Request.Builder ()
.url ("http://www.qq.com")
.build ()
2. Create an OkHttp client object:
OkHttpClient client = new OkHttpClient ()
3. Call the newCall method of the OkHttpClient object to get the Call object.
Call call = client.newCall (request)
If you need to execute network requests synchronously, execute 4, 5, 6:
If you need to execute asynchronously, network execution 4a: (recommended by Android)
-
4. Call the execute method of the call object, issue a network request, and get the Response object.
Response response = call.execute (); / / changing the method blocks the thread
5. Call the body method of response to get the corresponding volume.
ResponseBody body = response.body ()
6. Call the corresponding method of ResponseBody to get the specific response content.
Body.string (); / / if the result is a character type, call this method, which encodes utf-8 by default
Body.bytes (); / / returns a byte array
Body.byteStream (); / / returns the byte input stream.
-
4A, call the enqueue (CallBack) method of call to make the request method like the request queue.
Call.enqueue (new Callback () {
/ / callback after failed response
@ Override
Public void onFailure (Request request, IOException e) {
}
/ / callback after successful response
/ / Note that the callback for this method is in the child thread, so if you want to modify the UI, you must also use other hands / segments.
@ Override
Public void onResponse (Response response) throws IOException {
}
});
IV. Get request
Method 1: 1. Call the get () method of the Request.builder object to set the request method to "get" request.
Method 2. Call the method ("GET", null) method of the Request.builder object to set the request method to "get" request.
Note: the second parameter in the method indicates the request body, because the request parameter of the get request can be directly followed by the url, so the null can be passed in the get request.
5. Post request
Method 1: 1. Call the post (requestBody) method of the Request.builder object to set the request method to "get" request.
Method 2. Call the method ("POST", requestBody) method of the Request.builder object to set the request method to "get" request.
Note:
1. The request parameter (request body requestBody) of post must have, not null. If null, an exception is thrown.
2. About RequestBody:
RequestBody is an abstract class.
A: use RequestBody to submit key-value pairs:
RequestBody body = new FormEncodingBuilder ()
.add ("name", "zs")
.add ("pwd", "aaa")
.build ()
VI. Upload OkHttp files
File file = new File (Environment.getExternalStorageDirectory (), "a.mp4")
/ / application/octet-stream represents that the file is of binary type (any file is fine)
MediaType fileType = MediaType.parse ("application/octet-stream")
RequestBody body = RequestBody.create (fileType, file)
7. OkHttp uploads files and form data at the same time
File file = new File (Environment.getExternalStorageDirectory (), "a.mp4")
Log.e ("aaa", file+ "")
/ / application/octet-stream represents that the file is of binary type (any file is fine)
MediaType fileType = MediaType.parse ("application/octet-stream")
RequestBody body = RequestBody.create (fileType, file)
/ / create a MultipartBuilder object.
RequestBody body1 = new MultipartBuilder ()
.addFormDataPart ("music", "good music") / / normal form data
.addFormDataPart ("mp3", "apple.mp3", body) / / file
.build ()
The above is about the content of this article on "how to use OkHttp in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.