-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathOkHttpUtil.java
43 lines (39 loc) · 1.02 KB
/
OkHttpUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.h2t.study.util;
import com.alibaba.fastjson.JSONObject;
import com.h2t.study.interceptor.OkHttpTraceIdInterceptor;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
/**
* OkHttp工具类
*
* @author hetiantian
* @version 1.0
* @Date 2020/03/19 14:28
*/
public class OkHttpUtil {
private static OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(new OkHttpTraceIdInterceptor())
.build();
/**
* GET请求
*
* @param url 请求地址
* @return
*/
public static String doGet(String url) {
Request request = new Request.Builder()
.url(url)
.build();
final Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
return JSONObject.toJSONString(response.body());
}
}