|
@@ -25,6 +25,8 @@ import com.sybotan.android.demo.tools.GzipUtil
|
|
|
import com.sybotan.base.extensions.toJson
|
|
|
import okhttp3.*
|
|
|
import com.sybotan.base.utils.SJsonUtil
|
|
|
+import okhttp3.MediaType.Companion.toMediaType
|
|
|
+import okhttp3.RequestBody.Companion.toRequestBody
|
|
|
import java.io.File
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
@@ -40,19 +42,20 @@ object OkhttpUtil {
|
|
|
/** http客户端 */
|
|
|
private val httpClient: OkHttpClient by lazy {
|
|
|
val client = OkHttpClient().newBuilder()
|
|
|
- .readTimeout(600, TimeUnit.SECONDS)
|
|
|
- .writeTimeout(600 , TimeUnit.SECONDS)
|
|
|
- .connectTimeout(600 , TimeUnit.SECONDS)
|
|
|
+ .readTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .connectTimeout(600, TimeUnit.SECONDS)
|
|
|
// .cache(cache)
|
|
|
- .build()
|
|
|
+ .build()
|
|
|
client
|
|
|
}
|
|
|
|
|
|
/** 缓存 路径 */
|
|
|
private val cacheFile = File(DemoApp.getContext()!!.cacheDir, "cache")
|
|
|
+
|
|
|
/** 缓存时间*/
|
|
|
private val cache = Cache(cacheFile, (1024 * 1024 * 100).toLong()) //100Mb
|
|
|
- private val JSON = MediaType.parse("application/json; charset=utf-8")
|
|
|
+ private val JSON = "application/json; charset=utf-8".toMediaType()
|
|
|
|
|
|
/**
|
|
|
* post请求 json
|
|
@@ -60,21 +63,21 @@ object OkhttpUtil {
|
|
|
* 不能私有
|
|
|
*/
|
|
|
fun postRequestJson(url: String, json: String): String {
|
|
|
- val jsonBody = FormBody.create(JSON, json)
|
|
|
+ val jsonBody = json.toRequestBody(JSON)
|
|
|
val request = Request.Builder()
|
|
|
- .url(url)
|
|
|
- .post(jsonBody)
|
|
|
- .build()
|
|
|
+ .url(url)
|
|
|
+ .post(jsonBody)
|
|
|
+ .build()
|
|
|
val call = httpClient.newCall(request)
|
|
|
try {
|
|
|
val response = call.execute()
|
|
|
- response.code()
|
|
|
+ response.code
|
|
|
if (response.isSuccessful) {
|
|
|
// LogUtils.e("请求成功code${response.code()},url=${url}")
|
|
|
} else {
|
|
|
// LogUtils.e("请求成功code${response.code()},url=${url}")
|
|
|
}
|
|
|
- return response.body()!!.string()
|
|
|
+ return response.body!!.string()
|
|
|
} catch (e: Throwable) {
|
|
|
e.printStackTrace()
|
|
|
return ""
|
|
@@ -83,13 +86,14 @@ object OkhttpUtil {
|
|
|
} // Function postRequest()
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* post 对象
|
|
|
*/
|
|
|
- inline fun <reified T> postObj(url: String, requestBody: Any, namingPolicy: FieldNamingPolicy? = null): T {
|
|
|
+ inline fun <reified T> postObj(
|
|
|
+ url: String,
|
|
|
+ requestBody: Any,
|
|
|
+ namingPolicy: FieldNamingPolicy? = null
|
|
|
+ ): T {
|
|
|
val responseJson = postRequestJson(url, requestBody.toJson())
|
|
|
// LogUtils.e("数据 :${ responseJson.toJson()}")
|
|
|
return SJsonUtil.fromJson<T>(responseJson)
|
|
@@ -104,15 +108,14 @@ object OkhttpUtil {
|
|
|
} // Function postObj()
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* get 请求
|
|
|
*/
|
|
|
fun getRequest(url: String): String {
|
|
|
val request = Request.Builder()
|
|
|
- .url(url)
|
|
|
- .get()
|
|
|
- .build()
|
|
|
+ .url(url)
|
|
|
+ .get()
|
|
|
+ .build()
|
|
|
val call = httpClient.newCall(request)
|
|
|
try {
|
|
|
val response = call.execute()
|
|
@@ -121,7 +124,7 @@ object OkhttpUtil {
|
|
|
} else {
|
|
|
// LogUtils.e("请求失败code${response.code()},url=${url}")
|
|
|
}
|
|
|
- return response.body()!!.string()
|
|
|
+ return response.body!!.string()
|
|
|
} catch (e: Throwable) {
|
|
|
e.printStackTrace()
|
|
|
}
|
|
@@ -131,12 +134,12 @@ object OkhttpUtil {
|
|
|
/**
|
|
|
* get 请求
|
|
|
*/
|
|
|
- fun getRequest(url: String,projectId: String): String {
|
|
|
+ fun getRequest(url: String, projectId: String): String {
|
|
|
val request = Request.Builder()
|
|
|
- .url(url)
|
|
|
- .addHeader("ProjectId",projectId)
|
|
|
- .get()
|
|
|
- .build()
|
|
|
+ .url(url)
|
|
|
+ .addHeader("ProjectId", projectId)
|
|
|
+ .get()
|
|
|
+ .build()
|
|
|
val call = httpClient.newCall(request)
|
|
|
try {
|
|
|
val response = call.execute()
|
|
@@ -145,7 +148,7 @@ object OkhttpUtil {
|
|
|
} else {
|
|
|
// LogUtils.e("请求失败code${response.code()},url=${url}")
|
|
|
}
|
|
|
- return response.body()!!.string()
|
|
|
+ return response.body!!.string()
|
|
|
} catch (e: Throwable) {
|
|
|
e.printStackTrace()
|
|
|
}
|
|
@@ -162,10 +165,10 @@ object OkhttpUtil {
|
|
|
try {
|
|
|
var isGzip = false
|
|
|
val request = Request.Builder()
|
|
|
- .url(url)
|
|
|
- .get()
|
|
|
- .header("Accept-Encoding", "gzip")
|
|
|
- .build()
|
|
|
+ .url(url)
|
|
|
+ .get()
|
|
|
+ .header("Accept-Encoding", "gzip")
|
|
|
+ .build()
|
|
|
val call = httpClient.newCall(request)
|
|
|
|
|
|
val response = call.execute()
|
|
@@ -174,7 +177,7 @@ object OkhttpUtil {
|
|
|
} else {
|
|
|
// LogUtils.e("请求失败code${response.code()},url=${url}")
|
|
|
}
|
|
|
- return GzipUtil.uncompressToString(response.body()!!.bytes())
|
|
|
+ return GzipUtil.uncompressToString(response.body!!.bytes())
|
|
|
} catch (e: Exception) {
|
|
|
e.printStackTrace()
|
|
|
return ""
|
|
@@ -184,5 +187,4 @@ object OkhttpUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
}
|