123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package xn.update.http;
- import java.io.IOException;
- import okhttp3.Call;
- import okhttp3.MediaType;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.Response;
- import xn.update.http.bean.AllLogInterceptor;
- public enum OkHttpUtils {
- INSTANCE;
- private final OkHttpClient client = new OkHttpClient.Builder()
- .addInterceptor(new AllLogInterceptor())
- .build();
- private final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
- private OkHttpUtils() {
- }
- // 同步 GET 请求
- public Response getSync(String url) throws IOException {
- Request request = new Request.Builder()
- .url(url)
- .build();
- Call call = client.newCall(request);
- return call.execute();
- }
- // 同步 POST 请求
- public Response postSync(String url, String json) throws IOException {
- RequestBody body = RequestBody.create(JSON, json);
- Request request = new Request.Builder()
- .url(url)
- .post(body)
- .build();
- Call call = client.newCall(request);
- return call.execute();
- }
- }
|