|
@@ -26,15 +26,17 @@
|
|
|
|
|
|
package com.persagy.base.utils
|
|
|
|
|
|
-import com.alibaba.fastjson.PropertyNamingStrategy
|
|
|
+import com.fasterxml.jackson.databind.PropertyNamingStrategy
|
|
|
import com.persagy.base.extensions.toJson
|
|
|
import okhttp3.*
|
|
|
+import okhttp3.MediaType.Companion.toMediaType
|
|
|
+import okhttp3.RequestBody.Companion.toRequestBody
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
/**
|
|
|
- * Http 工具类
|
|
|
+ * Http工具类
|
|
|
*
|
|
|
- * @author 庞利祥 <sybotan@126.com>
|
|
|
+ * @author 庞利祥 <sybotan@126.com>
|
|
|
*/
|
|
|
object SHttpUtil {
|
|
|
/** 连接超时时间 */
|
|
@@ -44,7 +46,7 @@ object SHttpUtil {
|
|
|
/** 写入超时时间 */
|
|
|
var writeTimeout: Long = 30L
|
|
|
|
|
|
- /** http 客户端 */
|
|
|
+ /** http客户端 */
|
|
|
private val httpClient: OkHttpClient by lazy {
|
|
|
OkHttpClient.Builder()
|
|
|
.connectTimeout(connectTimeout, TimeUnit.SECONDS)
|
|
@@ -54,18 +56,18 @@ object SHttpUtil {
|
|
|
}
|
|
|
|
|
|
/** 媒体类型 */
|
|
|
- val ALTERNATIVE = MediaType.parse("multipart/alternative")
|
|
|
- val DIGEST = MediaType.parse("multipart/digest")
|
|
|
- val FORM = MediaType.parse("multipart/form-data")
|
|
|
- val JSON = MediaType.parse("application/json; charset=utf-8")
|
|
|
- val MIXED = MediaType.parse("multipart/mixed")
|
|
|
- val PARALLEL = MediaType.parse("multipart/parallel")
|
|
|
+ val ALTERNATIVE = "multipart/alternative".toMediaType()
|
|
|
+ val DIGEST = "multipart/digest".toMediaType()
|
|
|
+ val FORM = "multipart/form-data".toMediaType()
|
|
|
+ val JSON = "application/json; charset=utf-8".toMediaType()
|
|
|
+ val MIXED = "multipart/mixed".toMediaType()
|
|
|
+ val PARALLEL = "multipart/parallel".toMediaType()
|
|
|
|
|
|
/**
|
|
|
- * 根据扩展名获得 Http 的 Content-Type
|
|
|
+ * 根据扩展名获得Http的Content-Type
|
|
|
*
|
|
|
- * @param extensionName 文件扩展名
|
|
|
- * @return 扩展名对应的 Content-Type
|
|
|
+ * @param extensionName 文件扩展名
|
|
|
+ * @return 扩展名对应的Content-Type
|
|
|
*/
|
|
|
fun contentType(extensionName: String): String {
|
|
|
val map = mapOf(
|
|
@@ -411,10 +413,10 @@ object SHttpUtil {
|
|
|
"xap" to "application/x-silverlight-app"
|
|
|
)
|
|
|
|
|
|
- /** 扩展名转换为小写 */
|
|
|
+ // 扩展名转换为小写
|
|
|
val key = extensionName.toLowerCase()
|
|
|
|
|
|
- /** 如果 map 包含指定的扩展名,则返回对应的 Content-Type, 否则返回二进制流类型 application/octet-stream */
|
|
|
+ // 如果map包含指定的扩展名,则返回对应的Content-Type, 否则返回二进制流类型"application/octet-stream"
|
|
|
return if (map.contains(key)) {
|
|
|
map.getValue(key)
|
|
|
} else {
|
|
@@ -425,8 +427,8 @@ object SHttpUtil {
|
|
|
/**
|
|
|
* GET 请求
|
|
|
*
|
|
|
- * @param url 地址
|
|
|
- * @return 服务器返回的应答信息
|
|
|
+ * @param url URL 地址
|
|
|
+ * @return 服务器返回的应答信息
|
|
|
*/
|
|
|
@Throws(Exception::class)
|
|
|
fun getRequest(url: String): String {
|
|
@@ -437,13 +439,13 @@ object SHttpUtil {
|
|
|
|
|
|
val call = httpClient.newCall(request)
|
|
|
val response = call.execute()
|
|
|
- return response.body()!!.string()
|
|
|
+ return response.body!!.string()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * GET 请求(zip 压缩格式)
|
|
|
+ * GET 请求(zip压缩格式)
|
|
|
*
|
|
|
- * @param url 地址
|
|
|
+ * @param url URL地址
|
|
|
* @return 服务器返回的应答信息
|
|
|
*/
|
|
|
@Throws(Exception::class)
|
|
@@ -457,60 +459,82 @@ object SHttpUtil {
|
|
|
val call = httpClient.newCall(request)
|
|
|
val response = call.execute()
|
|
|
|
|
|
- return SGzipUtil.uncompressToString(response.body()!!.bytes()) ?: ""
|
|
|
+ return SGzipUtil.uncompressToString(response.body!!.bytes()) ?: ""
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发送 post 对象请求
|
|
|
+ * 发送post对象请求
|
|
|
*
|
|
|
- * @param url 地址
|
|
|
- * @param body 请求体
|
|
|
- * @return 返回应答体字符串
|
|
|
+ * @param url URL地址
|
|
|
+ * @param body 请求体
|
|
|
+ * @return 返回应答体字符串
|
|
|
*/
|
|
|
@Throws(Exception::class)
|
|
|
- fun postRequest(url: String, body: String, type: MediaType = JSON!!): String {
|
|
|
- val jsonBody = FormBody.create(type, body)
|
|
|
+ fun postRequest(url: String, body: String, type: MediaType = JSON): String {
|
|
|
+ val jsonBody = body.toRequestBody(type)
|
|
|
val request = createPostRequest(url, jsonBody)
|
|
|
+
|
|
|
val call = httpClient.newCall(request)
|
|
|
val response = call.execute()
|
|
|
- return response.body()!!.string()
|
|
|
+ return response.body!!.string()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发送 get 对象请求
|
|
|
+ * 发送get对象请求
|
|
|
*
|
|
|
- * @param <T> 请求的返回类型
|
|
|
- * @param url 地址
|
|
|
- * @param namingStrategy 命名规则
|
|
|
- * @return 接收到的请求体
|
|
|
+ * @param <T> 请求的返回类型
|
|
|
+ * @param url URL地址
|
|
|
+ * @param namingStrategy 命名规则
|
|
|
+ * @return 接收到的请求体
|
|
|
*/
|
|
|
@Throws(Exception::class)
|
|
|
inline fun <reified T> getObject(url: String, namingStrategy: PropertyNamingStrategy? = null): T {
|
|
|
val responseJson = getRequest(url)
|
|
|
- return SJsonUtil.fromJson(responseJson, namingStrategy)
|
|
|
+ return SJacksonUtil.fromJson<T>(responseJson, namingStrategy)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发送 post 请求
|
|
|
+ * 发送post请求
|
|
|
*
|
|
|
- * @param url 地址
|
|
|
- * @param <T> 请求的返回类型
|
|
|
- * @param requestBody 发送到服务器的请求体
|
|
|
- * @param namingStrategy 命名规则
|
|
|
- * @return 接收到的请求体
|
|
|
+ * @param url URL地址
|
|
|
+ * @param <T> 请求的返回类型
|
|
|
+ * @param requestBody 发送到服务器的请求体
|
|
|
+ * @param namingStrategy 命名规则
|
|
|
+ * @return 接收到的请求体
|
|
|
*/
|
|
|
@Throws(Exception::class)
|
|
|
inline fun <reified T> postObject(url: String, requestBody: Any, namingStrategy: PropertyNamingStrategy? = null): T {
|
|
|
val responseJson = postRequest(url, requestBody.toJson(namingStrategy))
|
|
|
- return SJsonUtil.fromJson(responseJson, namingStrategy)
|
|
|
+ return SJacksonUtil.fromJson(responseJson, namingStrategy)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ *
|
|
|
+ * @param url 上传地址
|
|
|
+ */
|
|
|
+ fun uploadFile(url: String) {
|
|
|
+ /*RequestBody requestBody = new MultipartBody.Builder()
|
|
|
+ .setType(MultipartBody.FORM)
|
|
|
+ .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file))
|
|
|
+ .build();*/
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 创建 Http post 请求对象
|
|
|
+ * 下载文件
|
|
|
*
|
|
|
- * @param url 地址
|
|
|
- * @param jsonBody 发送到服务器的请求体
|
|
|
- * @return 接收到的请求体
|
|
|
+ * @param url 文件url地址
|
|
|
+ */
|
|
|
+ fun downloadFile(url: String) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建Http post请求对象
|
|
|
+ *
|
|
|
+ * @param url URL地址
|
|
|
+ * @param jsonBody 发送到服务器的请求体
|
|
|
*/
|
|
|
private fun createPostRequest(url: String, jsonBody: RequestBody? = null): Request {
|
|
|
val builder = Request.Builder()
|
|
@@ -521,5 +545,4 @@ object SHttpUtil {
|
|
|
|
|
|
return builder.build()
|
|
|
}
|
|
|
-
|
|
|
}
|