http request tool - OkHttp usage

Introduction to OKHttp

okhttp is a third-party library for requesting network in android. This is an open source project, the hottest lightweight framework on Android, contributed by Square, a mobile payment company (which also contributed to Picasso and LeakCanary). Used to replace HttpUrlConnection and Apache HttpClient (HttpClient has been removed in android API23). okhttp has its own official website, the official website URL: OKHttp official website If you want to know the original code, you can download it on github, the address is: https://github.com/square/okhttp You don't need to download the jar package to use it in AndroidStudio, just add dependencies directly Available: compile 'com.squareup.okhttp3:okhttp:3.4.1'

In development, we often need to use http requests, here is a brief introduction to the usage of an http request tool okHttp

1. Import the 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 future use, a tool class of OkHttpUtil is encapsulated here

get request

 1 /**
 2  * get请求
 3  * @param url 请求地址
 4  * @return 请求结果
 5  */
 6 public String doGet(String url) {
 7     OkHttpClient okHttpClient = new OkHttpClient();
 8     Request request = new Request.Builder().url(url).build();
 9     Call call = okHttpClient.newCall(request);
10     try {
11         Response response = call.execute();
12         return response.body().string();
13     } catch (IOException e) {
14         e.printStackTrace();
15     }
16     return null;
17  }

There are two types of post requests, From form form and JSON parameter form

  • Form form

1  /** 
2  * Form post request
 3  * @param url request address
 4  * @param map post request parameter
 5  * @return request result
 6   */ 
7  public String doPost(String url,Map<String,String> map) {
 8      OkHttpClient client = new OkHttpClient();
 9      // Build a formBody builder 
10      FormBody.Builder builder = new FormBody.Builder();
 11      // Circulate the form form and add the form content to the form builder 
12      for (Map. Entry<String,String>entry : map.entrySet()) {
 13          String key = entry.getKey();
 14          String value = entry.getValue();
 15          builder.add(key,value);
 16      }
 17      // Build formBody and pass it 
18      FormBody body = builder.build();
 19 Request      request = new Request.Builder().url(url).post(body).build();
 20      Call call = client.newCall(request);
 21      // Return request result 
22      try {
 23          Response response = call.execute();
 24         return response.body().string();
25     } catch (IOException e) {
26         e.printStackTrace();
27     }
28     return null;
29 }
  • JSON parameter form
1  /** 
2  * Post request in the form of Json body
 3  * @param url request address
 4  * @return request result
 5   */ 
6  public String doPost(String url,String json){
 7      OkHttpClient client = new OkHttpClient();
 8      RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8" ), json);
 9      Request request = new Request.Builder()
 10              .post(body)
 11              .url(url).
 12                      build();
 13     Call call = client.newCall(request);
14     //返回请求结果
15     try {
16         Response response = call.execute();
17         return response.body().string();
18     } catch (IOException e) {
19         e.printStackTrace();
20     }
21     return null;
22 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569748&siteId=291194637