package http; import java.io.IOException; import okhttp3.Call; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class OkHttpUtils { private static final OkHttpClient client = HttpClient.buildHttpClient(); private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private OkHttpUtils() { } // 同步 GET 请求 public static Response getSync(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Call call = client.newCall(request); return call.execute(); } // 同步 POST 请求 public static 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(); } }