Okhttp Introduction
Okhttp is a third-party class library for requesting networks in Android. This is an open source project and is Android's hottest lightweight framework, contributed by the mobile payment Square company (the company also contributed Picasso and Leakcanary). Used in place of HttpURLConnection and Apache HttpClient (HttpClient removed from Android API23).
Okhttp has its own official website, website: okhttp official website If you want to know the original code can be downloaded on GitHub, the address is: https://github.com/square/okhttp
No need to download jar packages in Androidstudio, add dependencies directly: Compile ' com.squareup.okhttp3:okhttp:3.4.1 '
In the development we will often need to use the HTTP request, here is a brief introduction of an HTTP request tool Okhttp Usage
1. Import Jar Package
1 <dependency>2 <groupId>com.squareup.okhttp3</groupId>3 <artifactId>okhttp</artifactId>4 <version>3.9.1</version>5 </ Dependency>
2, in order to facilitate later use, here encapsulates a okhttputil tool class
GET Request
1 /**2 * GET Request3 * @paramURL Request Address4 * @returnRequest Result5 */6 Publicstring doget (string url) {7Okhttpclient okhttpclient =Newokhttpclient ();8Request Request =Newrequest.builder (). URL (URL). build ();9Call call =Okhttpclient.newcall (request);Ten Try { OneResponse Response =Call.execute (); A returnresponse.body (). String (); -}Catch(IOException e) { - e.printstacktrace (); the } - return NULL; -}
Post requests are divided into two types, from form and JSON parameter form
1 /**2 * form-form POST request3 * @paramURL Request Address4 * @parammap POST Request Parameters5 * @returnRequest Result6 */7 PublicString DoPost (String url,map<string,string>map) {8Okhttpclient client =Newokhttpclient ();9 //build a formbody builderTenFormbody.builder Builder =NewFormbody.builder (); One //Loop form form to add form content to form builder A for(map.entry<string,string>Entry:map.entrySet ()) { -String key =Entry.getkey (); -String value =Entry.getvalue (); the Builder.add (key,value); - } - //build the Formbody and pass it into request requests -Formbody BODY =builder.build (); +Request Request =Newrequest.builder (). URL (URL). Post (body). Build (); -Call call =Client.newcall (request); + //return request Result A Try { atResponse Response =Call.execute (); - returnresponse.body (). String (); -}Catch(IOException e) { - e.printstacktrace (); - } - return NULL; in}
1 /**2 * Json body form of POST request3 * @paramURL Request Address4 * @returnRequest Result5 */6 Publicstring DoPost (String url,string json) {7Okhttpclient client =Newokhttpclient ();8Requestbody BODY = requestbody.create (Mediatype.parse ("Application/json; Charset=utf-8 "), JSON);9Request Request =NewRequest.builder ()Ten . Post (body) One . URL (url). A build (); -Call call =Client.newcall (request); - //return request Result the Try { -Response Response =Call.execute (); - returnresponse.body (). String (); -}Catch(IOException e) { + e.printstacktrace (); - } + return NULL; A}
HTTP request Tool-okhttp Usage