HTTP request Tool-okhttp Usage

Source: Internet
Author: User
Tags form post

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
    • Form form Forms

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}
    • JSON Parameter form
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

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.