|
@@ -6,8 +6,8 @@ import android.os.Looper
|
|
import android.os.Message
|
|
import android.os.Message
|
|
import com.cr.data.*
|
|
import com.cr.data.*
|
|
import com.cr.dialog.DialogLoadingUtil
|
|
import com.cr.dialog.DialogLoadingUtil
|
|
|
|
+import com.cr.dialog.DialogNormal
|
|
import com.cr.models.iNetDataModel
|
|
import com.cr.models.iNetDataModel
|
|
-import dji.v5.utils.common.ContextUtil
|
|
|
|
import okhttp3.*
|
|
import okhttp3.*
|
|
import okhttp3.MediaType.Companion.toMediaType
|
|
import okhttp3.MediaType.Companion.toMediaType
|
|
import okhttp3.RequestBody.Companion.toRequestBody
|
|
import okhttp3.RequestBody.Companion.toRequestBody
|
|
@@ -15,6 +15,7 @@ import org.json.JSONArray
|
|
import org.json.JSONException
|
|
import org.json.JSONException
|
|
import org.json.JSONObject
|
|
import org.json.JSONObject
|
|
import java.io.IOException
|
|
import java.io.IOException
|
|
|
|
+import java.io.InputStream
|
|
import java.net.ConnectException
|
|
import java.net.ConnectException
|
|
import java.util.concurrent.TimeUnit
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
@@ -28,6 +29,10 @@ class TCPDataTask {
|
|
// define: 2023/4/10 定义显示或关闭等待条
|
|
// define: 2023/4/10 定义显示或关闭等待条
|
|
val PROGRESS_SHOW:Int = 1001
|
|
val PROGRESS_SHOW:Int = 1001
|
|
val PROGRESS_CLOSE:Int = 1002
|
|
val PROGRESS_CLOSE:Int = 1002
|
|
|
|
+ val SHOW_MESSAGE:Int = 1003
|
|
|
|
+ var DOWNLOAD_ERROR:Int = 1004
|
|
|
|
+ var DOWNLOAD_PROGRESS:Int = 1005
|
|
|
|
+ var DOWNLOAD_COMPLETE:Int = 1006
|
|
|
|
|
|
// define: 2023/4/10 定义变量
|
|
// define: 2023/4/10 定义变量
|
|
var context:Context? = null
|
|
var context:Context? = null
|
|
@@ -42,6 +47,8 @@ class TCPDataTask {
|
|
DialogLoadingUtil.show(context!!,msg.obj.toString())
|
|
DialogLoadingUtil.show(context!!,msg.obj.toString())
|
|
}else if(msg.what == PROGRESS_CLOSE){
|
|
}else if(msg.what == PROGRESS_CLOSE){
|
|
DialogLoadingUtil.dismiss()
|
|
DialogLoadingUtil.dismiss()
|
|
|
|
+ }else if(msg.what == SHOW_MESSAGE){
|
|
|
|
+ DialogNormal(context!!,"警告",msg.obj.toString()).show()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -53,6 +60,24 @@ class TCPDataTask {
|
|
fun onSuccess(jsonArray:JSONArray)
|
|
fun onSuccess(jsonArray:JSONArray)
|
|
fun onFailed(message:String)
|
|
fun onFailed(message:String)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 下载监听
|
|
|
|
+ */
|
|
|
|
+ interface OnDownloadListener{
|
|
|
|
+ // todo: 2023/4/12 开始下载
|
|
|
|
+ fun onStart()
|
|
|
|
+ // todo: 2023/4/12 下载失败
|
|
|
|
+ fun onFailed(message:String)
|
|
|
|
+
|
|
|
|
+ // todo: 2023/4/12 下载进度
|
|
|
|
+ fun onProgress(progress:Int,total:Int)
|
|
|
|
+
|
|
|
|
+ // todo: 2023/4/12 下载完成
|
|
|
|
+ fun onComplete()
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
// todo: 2023/4/8 网络连接
|
|
// todo: 2023/4/8 网络连接
|
|
var okHttp: OkHttpClient? = null
|
|
var okHttp: OkHttpClient? = null
|
|
|
|
|
|
@@ -103,6 +128,17 @@ class TCPDataTask {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 显示提示消息
|
|
|
|
+ * @param message String
|
|
|
|
+ */
|
|
|
|
+ private fun showMessage(message:String){
|
|
|
|
+ var msg:Message = Message();
|
|
|
|
+ msg.what = SHOW_MESSAGE
|
|
|
|
+ msg.obj = message
|
|
|
|
+ handler.sendMessage(msg)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
*
|
|
*
|
|
* @param url String 服务地址
|
|
* @param url String 服务地址
|
|
* @param iModel iNetDataModel<T> 发送数据
|
|
* @param iModel iNetDataModel<T> 发送数据
|
|
@@ -160,6 +196,56 @@ class TCPDataTask {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fun sendDownloadFile(context: Context,fileName:String,url:String,callBack:OnDownloadListener){
|
|
|
|
+ if(callBack != null) callBack.onStart()
|
|
|
|
+ // todo: 2023/4/12 上下文
|
|
|
|
+ this.context = context
|
|
|
|
+ // todo: 2023/4/12 定义下载的Url
|
|
|
|
+ var request:Request = Request.Builder().url(url).build()
|
|
|
|
+ // todo: 2023/4/12 开始调用下载
|
|
|
|
+ okHttp?.newCall(request)?.enqueue(object:Callback{
|
|
|
|
+ // todo: 2023/4/12 下载失败
|
|
|
|
+ override fun onFailure(call: Call, e: IOException) {
|
|
|
|
+ handler.post(Runnable {
|
|
|
|
+ if(callBack!= null) {
|
|
|
|
+ callBack.onComplete()
|
|
|
|
+ callBack.onFailed(e.message!!)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // todo: 2023/4/12 下载成功
|
|
|
|
+ override fun onResponse(call: Call, response: Response) {
|
|
|
|
+ var inputStream:InputStream? = null
|
|
|
|
+ var buf = ByteArray(2048)
|
|
|
|
+ var len:Int
|
|
|
|
+ try{
|
|
|
|
+ inputStream = response.body!!.byteStream()
|
|
|
|
+ var total:Long = response.body!!.contentLength()
|
|
|
|
+ var sum:Long = 0
|
|
|
|
+ while (true){
|
|
|
|
+ len = inputStream.read(buf)
|
|
|
|
+ if(len == -1) break
|
|
|
|
+ sum += len
|
|
|
|
+ handler.post(Runnable {
|
|
|
|
+ if(callBack != null) callBack.onProgress(sum.toInt(), total.toInt())
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }catch (e: IOException){
|
|
|
|
+ handler.post(Runnable {
|
|
|
|
+ if(callBack!= null) callBack.onFailed(e.message!!)
|
|
|
|
+ })
|
|
|
|
+ }finally {
|
|
|
|
+ inputStream!!.close()
|
|
|
|
+ handler.post(Runnable {
|
|
|
|
+ if(callBack != null) callBack.onComplete()
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 解析JSON数据
|
|
* 解析JSON数据
|
|
* @param JSON String JSON字符串
|
|
* @param JSON String JSON字符串
|