OkHttp 3 is a relatively popular HTTP client, which is used to initiate HTTP requests.
Here are some of the features it supports:
- Support to initiate PUT, DELETE, POST, GET and other requests
- Support file upload and download
- Load the picture so far (the picture size will be automatically compressed internally)
- Support request callback, directly return object, object collection
- Support session maintenance
Let's record the most common GET requests and POST requests using OkHttp 3.
Initiate a GET request
In this form, the parameters are spliced directly behind the url, the first parameter is connected with ?, and each subsequent parameter is connected with &.
/**
* HTTP接口-GET方式,请求参数形式为params形式
*
* @param url
* @param param
* @return String
*/
public static String sendGet(String url, String param) throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(180, TimeUnit.SECONDS)
.readTimeout(180, TimeUnit.SECONDS)
.writeTimeout(180, TimeUnit.SECONDS)
.build();
String dataUrl = url + "?userName=" + param;
Request request = new Request.Builder()
.url(dataUrl)
.get()
.build();
Response response;
String result;
try {
response = client.newCall(request).execute();
result = response.body().string();
} catch (IOException e) {
throw new IOException(DATA, e);
}
return result;
}
Initiate a POST request
The parameter is a POST request in the form of a form
/**
* 表单形式post请求
*
* @paramurl 请求地址
* @parammap post请求参数
* @return请求结果
*/
public static String doPostWithForm(Map<String, String> map, String url) throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(180, TimeUnit.SECONDS)
.readTimeout(180, TimeUnit.SECONDS)
.writeTimeout(180, TimeUnit.SECONDS)
.build();
//构建一个formBody builder
FormBody.Builder builder = new FormBody.Builder();
//循环form表单,将表单内容添加到form builder中
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
builder.add(key, value);
}
//构建formBody,将其传入Request请求中
FormBody body = builder.build();
Request request = new Request.Builder().url(url).post(body).build();
Call call = client.newCall(request);
//返回请求结果
try (Response response = call.execute()) {
return response.body().string();
} catch (IOException e) {
throw new IOException(e);
}
}
The parameter is a POST request in the form of json
/**
* HTTP接口-POST方式,请求参数形式为body-json形式
*
* @param url
* @param jsonString
* @return String
*/
public static String sendPostWithJson(String url, String jsonString) throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder().build();
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonString);
Request request = new Request.Builder()
.post(body)
.url(url)
.build();
Call call = client.newCall(request);
//返回请求结果
try {
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
throw new IOException(e);
}
}
The parameters are directly spliced in the POST request after the url
This form of parameter passing is similar to GET requests.
/**
* HTTP接口-POST方式,请求参数形式为params形式
*
* @param url
* @param param
* @return String
*/
public static String sendPost(String url, String param) throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(180, TimeUnit.SECONDS)
.readTimeout(180, TimeUnit.SECONDS)
.writeTimeout(180, TimeUnit.SECONDS)
.build();
FormBody body = new FormBody.Builder().build();
String dataUrl = url + "?userName=" + param;
Request request = new Request.Builder()
.url(dataUrl)
.post(body)
.build();
Response response;
String result;
try {
response = client.newCall(request).execute();
result = response.body().string();
} catch (IOException e) {
throw new IOException(e);
}
return result;
}