赞
踩
OkHttp 3 是一个比较流行的 HTTP 客户端,作用就是用来发起 HTTP 请求的。
以下是它支持的一些功能:
下面记录一下用 OkHttp 3 ,发起最常见的 GET 请求和 POST 请求。
这种形式,参数直接拼接在 url 后面,第一个参数用 ? 连接,后面的每一个参数都用 & 连接。
/**
* 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;
}
/**
* 表单形式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);
}
}
/**
* 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);
}
}
这种形式的参数传递,和 GET 请求类似。
/**
* 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;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。