@File
2019-09-02T07:12:32.000000Z
字数 4209
阅读 139
java
<!-- okhttp3 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.2</version>
</dependency>
// okhttp对象
OkHttpClient okc = new OkHttpClient();
// 获取请求发送对象(GET)
Call call = okc.newCall(
// 请求配置
new Request
.Builder()
.url("http://10.3.135.58:8080/api/company")
.build()
);
- MediaType.parse
- json : application/json
- xml : application/xml
- png : image/png
- jpg : image/jpeg
- gif : imge/gif
- md : text/x-markdown
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "请求内容");
RequestBody requestBody = new RequestBody() {
@Nullable
@Override
public MediaType contentType() {
// 请求内容类型
return MediaType.parse("text/x-markdown; charset=utf-8");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
// 请求内容
sink.writeUtf8("hello!");
}
};
RequestBody requestBody = new FormBody
.Builder()
.add("key","value")
.add("key","value")
Call call = okc.newCall(
// 请求配置
new Request
.Builder()
.post(requestBody) // 声明请求类型
.url("http://10.3.135.58:8080/api/company")
.build()
);
execute()
同步请求Call
请求发送对象
try {
// 发送请求,拿到回调内容对象
ResponseBody resBody = call.execute().body();
// 以字符串形式取出
String resStr = resBody.string();
} catch (IOException e) {
e.printStackTrace();
}
enqueue()
异步请求Call
请求发送对象
call.enqueue(new Callback() {
// 请求失败
@Override
public void onFailure(Call call, IOException e) {
// 失败执行的操作
System.out.println(e.getMessage());
}
// 请求成功
@Override
public void onResponse(Call call, Response res) throws IOException {
// 成功执行的操作
System.out.println(res.body().string());
}
});
public class RequestTool {
/**
* get 请求
* @param url 请求地址
* @return 相应内容
* @throws IOException IO异常
*/
public static String get(String url) throws IOException {
return respString(url,null);
}
/**
* post 带参请求
* @return 请求对象
*/
public static Req post(){
return new Req();
}
/**
* post 请求
* @param url 请求地址
* @return 相应内容
* @throws IOException IO异常
*/
public static String post(String url) throws IOException {
return new Req().send(url);
}
/**
* 发起请求,获取相应的字符串
* @param url 请求连接
* @param body 请求体(post才需要)
* @return 相应体对象
* @throws IOException IO异常
*/
private static String respString(String url,RequestBody body) throws IOException {
return respBody(url,body).string();
}
/**
* 获取相应体对象
* @param url 请求连接
* @param body 请求体(post才需要)
* @return 相应体对象
* @throws IOException IO异常
*/
private static ResponseBody respBody(String url,RequestBody body) throws IOException {
OkHttpClient okc = new OkHttpClient();
Request.Builder request = new Request
.Builder()
.url(url);
if(body != null){
request.post(body);
}
Call call = okc.newCall(request.build());
return call.execute().body();
}
/**
* 请求对象
*/
public static class Req{
private MediaType mediaType;
private FormBody.Builder builder;
private File file;
/**
* 给请求体添加参数
* @param key 参数名
* @param value 参数值
* @return 当前请求对象
*/
public Req param(String key,String value) {
if(builder == null){builder = new FormBody.Builder();}
builder.add(key,value);
return this;
}
/**
* 发送文件
* @param file 文件对象
* @param mediaType 文件类型
* @return 当前请求对象
*/
public Req file(File file,String mediaType){
this.file = file;
this.mediaType = MediaType.parse(mediaType);
return this;
}
/**
* 发送请求
* @return 相应内容
* @throws IOException IO异常
*/
public String send(String url) throws IOException {
// 表单
if(builder == null){
return respString(url,builder.build());
}
// 文件
if(file != null && mediaType != null){
return respString(url,RequestBody.create(mediaType,file));
}
// 空请求
return respString(url,RequestBody.create(
MediaType.parse("application/json; charset=utf-8"),
""
));
}
}
}
// get 请求,参数自己拼接到 url 上
String res = RequestTool.get("http://localhost:8080/okhttp/GetReq");
// 无参 post 请求
String res = RequestTool.post("http://localhost:8080/okhttp/PostReq");
// 有参 post 请求
String res = RequestTool
.post()
.param("key","value")
.param("id","10")
.send("http://localhost:8080/okhttp/PostReq");
// post 文件请求(有参数请求时文件会失效,且未测试过)
String res = RequestTool
.post()
.file(new File("lidaye.img"),"image/jpeg;charset=utf-8")
.send("http://localhost:8080/okhttp/PostReq");
// 自定义 post 请求(自定义RequestBody)
String res = RequestTool.respString("http://localhost:8080/okhttp/PostReq", new RequestBody() {
@Override
public MediaType contentType() {
// 请求内容类型
return MediaType.parse("text/x-markdown; charset=utf-8");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
// 请求内容
sink.writeUtf8("hello!");
}
});