封装okhttp
整理一次完整的http封装,支持get/post/applicatiopn.json/forme表单等请求方式
首先pom中引入okhttp包
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.4.1</version> </dependency>
封装接口
public interface HttpClient {
/**
* 设置底层读超时,以毫秒为单位。值0指定无限超时。
*
* @see okhttp3.OkHttpClient.Builder#readTimeout(long, TimeUnit)
*/
public void setReadTimeout(int readTimeout);
/**
* 设置底层连接超时,以毫秒为单位。值0指定无限超时。
*
* @see okhttp3.OkHttpClient.Builder#connectTimeout(long, TimeUnit)
*/
public void setConnectTimeout(int connectTimeout);
/**
* 设置底层写超时,以毫秒为单位。值0指定无限超时。
*
* @see okhttp3.OkHttpClient.Builder#writeTimeout(long, TimeUnit)
*/
public void setWriteTimeout(int writeTimeout);
/**
* 发起一个GET请求
*
* @param url : 请求地址,需要用 {@link URI} 封装,这样如果地址错了可以在请求前解析出来
* @return
* @throws IOException
*/
public String get(@NonNull String url) throws Exception;
/**
* 发起一个GET请求
*
* @param uri : 请求地址,需要用 {@link URI} 封装,这样如果地址错了可以在请求前解析出来
* @return
* @throws IOException
*/
public String get(URI uri) throws Exception;
/**
* 发起一个GET请求
*
* @param url 请求地址,需要用 {@link URI} 封装,这样如果地址错了可以在请求前解析出来
* @param headerName
* @param headerValue
* @return
* @throws IOException
*/
public String get(@NonNull String url, String headerName, String headerValue) throws Exception;
/**
* 发起一个GET请求
*
* @param uri 请求地址,需要用 {@link URI} 封装,这样如果地址错了可以在请求前解析出来
* @param headerName
* @param headerValue
* @return
* @throws IOException
*/
public String get(URI uri, String headerName, String headerValue) throws Exception;
/**
* 发起一个GET请求
*
* @param url 请求地址,需要用 {@link URI} 封装,这样如果地址错了可以在请求前解析出来
* @param headers
* @return
* @throws IOException
*/
public String get(@NonNull String url, Map<String, String> headers) throws Exception;
/**
* 发起一个GET请求
*
* @param uri 请求地址,需要用 {@link URI} 封装,这样如果地址错了可以在请求前解析出来
* @param headers
* @return
* @throws IOException
*/
public String get(@NonNull URI uri, Map<String, String> headers) throws Exception;
/**
* 发起一个 自定义contentType的请求
*
* @param uri : 请求地址
* @param httpMethod : 请求方法 {@link HttpMethod}
* @param contentType : 请求类型
* @param content : 请求内容
* @return
* @throws IOException
*/
public String post(@NonNull URI uri, HttpMethod httpMethod, MediaType contentType, String content)
throws Exception;
/**
* 发起一个 application/json; charset=utf-8 请求
*
* @param url : 请求地址
* @param content : 请求内容
* @return
* @throws IOException
*/
public String post(@NonNull String url, String content) throws Exception;
/**
* 发起一个 application/json; charset=utf-8 请求
*
* @param uri : 请求地址
* @param httpMethod : 请求方法 {@link HttpMethod}
* @param content : 请求内容
* @return
* @throws IOException
*/
public String postRequest(@NonNull URI uri, HttpMethod httpMethod, String content) throws Exception;
/**
* 发起一个 application/json; charset=utf-8 请求,并携带请求头
*
* @param url
* @param content
* @param headers
* @return
* @throws IOException
*/
public String postWithHeader(@NonNull String url, String content, Map<String, String> headers) throws Exception;
/**
* 发起一个 application/json; charset=utf-8 请求,并携带请求头
*
* @param uri
* @param httpMethod
* @param content
* @param headers
* @return
* @throws IOException
*/
public String postWithHeader(@NonNull URI uri, HttpMethod httpMethod, String content, Map<String, String> headers) throws Exception;
/**
* 发起一个 application/json; charset=utf-8 请求,并携带请求头
*
* @param url
* @param headers
* @return
* @throws IOException
*/
public String postWithHeader(@NonNull String url, Map<String, String> headers) throws Exception;
/**
* 发起一个以 application/json; charset=utf-8 形式的请求
*
* @param ur

本文详细介绍了如何封装OkHttp,包括支持GET、POST、application.json和forme表单请求。首先在POM中引入OkHttp依赖,然后封装接口和实现,接着通过工厂Bean进行封装,并使用常量进行配置。最后展示了如何注入Bean并实现在Java或XML配置中加载封装好的OkHttp客户端。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



