|
@@ -5,19 +5,25 @@ import android.text.TextUtils
|
|
|
import cn.yyxx.eyuangame.base.utils.Logger
|
|
|
import cn.yyxx.eyuangame.core.entity.ResultInfo
|
|
|
import cn.yyxx.eyuangame.core.impl.SdkDrive
|
|
|
+import cn.yyxx.eyuangame.core.internal.IFileRequestCallback
|
|
|
import cn.yyxx.eyuangame.core.internal.IRequestCallback
|
|
|
+import cn.yyxx.support.FileUtils
|
|
|
import cn.yyxx.support.JsonUtils
|
|
|
import cn.yyxx.support.StrUtils
|
|
|
import cn.yyxx.support.encryption.Base64Utils
|
|
|
import cn.yyxx.support.encryption.HexUtils
|
|
|
import cn.yyxx.support.encryption.Md5Utils
|
|
|
import cn.yyxx.support.volley.VolleySingleton
|
|
|
-import cn.yyxx.support.volley.source.DefaultRetryPolicy
|
|
|
-import cn.yyxx.support.volley.source.Response
|
|
|
-import cn.yyxx.support.volley.source.VolleyError
|
|
|
+import cn.yyxx.support.volley.source.*
|
|
|
+import cn.yyxx.support.volley.source.toolbox.HttpHeaderParser
|
|
|
import cn.yyxx.support.volley.source.toolbox.JsonObjectRequest
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.GlobalScope
|
|
|
+import kotlinx.coroutines.launch
|
|
|
import org.json.JSONException
|
|
|
import org.json.JSONObject
|
|
|
+import java.io.File
|
|
|
+import java.io.IOException
|
|
|
|
|
|
/**
|
|
|
* @author #Suyghur.
|
|
@@ -102,13 +108,58 @@ object VolleyRequest {
|
|
|
}
|
|
|
//设置超时时间
|
|
|
request.retryPolicy = DefaultRetryPolicy(MAX_TIMEOUT, 1, 1.0f)
|
|
|
- VolleySingleton.getInstance(context.applicationContext)
|
|
|
- .addToRequestQueue(context.applicationContext, request)
|
|
|
+ VolleySingleton.getInstance(context.applicationContext).addToRequestQueue(context.applicationContext, request)
|
|
|
} catch (e: Exception) {
|
|
|
e.printStackTrace()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fun downloadImageFile(context: Context, url: String, callback: IFileRequestCallback) {
|
|
|
+ val cacheFolder = File(context.getExternalFilesDir("cache")!!.absolutePath)
|
|
|
+ if (!cacheFolder.exists()) {
|
|
|
+ cacheFolder.mkdirs()
|
|
|
+ }
|
|
|
+ val fileName = Md5Utils.encodeByMD5(url)
|
|
|
+ val filePath = "$cacheFolder/$fileName"
|
|
|
+ Logger.d(filePath)
|
|
|
+ if (File(filePath).exists()) {
|
|
|
+ callback.onResponse("image has been cached locally")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ val request: Request<ByteArray> = object : Request<ByteArray>(Method.GET, url, Response.ErrorListener {
|
|
|
+ callback.onErrorResponse(it)
|
|
|
+ }) {
|
|
|
+ override fun parseNetworkResponse(response: NetworkResponse): Response<ByteArray> {
|
|
|
+ return try {
|
|
|
+ if (response.data == null) {
|
|
|
+ Response.error(ParseError(response))
|
|
|
+ } else {
|
|
|
+ Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response))
|
|
|
+ }
|
|
|
+ } catch (e: OutOfMemoryError) {
|
|
|
+ e.printStackTrace()
|
|
|
+ Response.error(ParseError(e))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun deliverResponse(response: ByteArray) {
|
|
|
+ Logger.d("volley download image file success, start to save file ...")
|
|
|
+ try {
|
|
|
+ GlobalScope.launch(Dispatchers.IO){
|
|
|
+ Logger.d("with io coroutines do save file")
|
|
|
+ FileUtils.saveFile(filePath, response)
|
|
|
+ }
|
|
|
+ callback.onResponse("download file success, path: $filePath")
|
|
|
+ } catch (e: IOException) {
|
|
|
+ e.printStackTrace()
|
|
|
+ callback.onErrorResponse(ParseError(e))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ VolleySingleton.getInstance(context.applicationContext).addToRequestQueue(context.applicationContext, request)
|
|
|
+ }
|
|
|
+
|
|
|
private fun parseResponse(context: Context, data: JSONObject): String {
|
|
|
try {
|
|
|
val ts = data.getString("ts")
|