From 8500eaf3857d559fb18210faa69cbb575b4f74b3 Mon Sep 17 00:00:00 2001 From: xiaowusky Date: Tue, 6 Jun 2023 10:57:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E5=BF=83=E8=B7=B3=E5=92=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 + .../safetywatcher/watcher/net/HeartbeatApi.kt | 32 ++++++++ .../watcher/net/UploadFileApi.kt | 76 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 app/src/main/java/com/yinuo/safetywatcher/watcher/net/HeartbeatApi.kt create mode 100644 app/src/main/java/com/yinuo/safetywatcher/watcher/net/UploadFileApi.kt diff --git a/app/build.gradle b/app/build.gradle index dcbbaf9..cb9b552 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -56,6 +56,10 @@ dependencies { implementation 'com.github.loper7:DateTimePicker:0.6.3' //ijk implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8' + // 添加retrofit依赖 + implementation rootProject.ext.dependencies.retrofit + // 添加rxjava依赖 + implementation rootProject.ext.dependencies.rxjava implementation 'com.google.android.material:material:1.5.0' annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.0.0' diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/net/HeartbeatApi.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/net/HeartbeatApi.kt new file mode 100644 index 0000000..7eac53c --- /dev/null +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/net/HeartbeatApi.kt @@ -0,0 +1,32 @@ +package com.yinuo.safetywatcher.watcher.net + +import com.common.commonlib.net.BaseObserve +import com.common.commonlib.net.bean.BaseResponse +import com.common.commonlib.net.callback.RequestNoResultCallBack +import io.reactivex.rxjava3.core.Observable +import retrofit2.http.FormUrlEncoded +import retrofit2.http.GET +import retrofit2.http.Query + +/** + * 心跳 + */ +class HeartbeatApi : BaseObserve(Api::class.java) { + + open fun heartBeat(sn: String) { + observe(api.heartBeat(sn), object : RequestNoResultCallBack() { + override fun onError(error: String?) { + } + + override fun onComplete() { + } + }) + } + + + interface Api { + @GET("xx/xxx") + @FormUrlEncoded + fun heartBeat(@Query("sn") sn: String): Observable + } +} \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/net/UploadFileApi.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/net/UploadFileApi.kt new file mode 100644 index 0000000..e8411cb --- /dev/null +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/net/UploadFileApi.kt @@ -0,0 +1,76 @@ +package com.yinuo.safetywatcher.watcher.net + +import com.common.commonlib.net.BaseObserve +import com.common.commonlib.net.bean.BaseResponse +import com.common.commonlib.net.callback.RequestResultCallBack +import io.reactivex.rxjava3.core.Observable +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.MultipartBody +import okhttp3.RequestBody +import okhttp3.RequestBody.Companion.asRequestBody +import retrofit2.http.Multipart +import retrofit2.http.POST +import retrofit2.http.Part +import retrofit2.http.PartMap +import java.io.File + + +/** + * 文件上传 + */ +class UploadFileApi : BaseObserve(Api::class.java) { + + open fun singleUpload(path: String) { + observe( + api.singleUpload(makeMultipartBodyPart(path)), + object : RequestResultCallBack() { + override fun onResult(result: BaseResponse) { + } + + override fun onError(error: String?) { + } + }) + } + + open fun multiUpload(paths: List) { + observe( + api.multiUpload(makefileMaps(paths)), + object : RequestResultCallBack() { + override fun onResult(result: BaseResponse) { + } + + override fun onError(error: String?) { + } + }) + } + + + interface Api { + @Multipart + @POST("xxx/upload") + fun singleUpload(@Part file: MultipartBody.Part?): Observable + + @Multipart + @POST("xxx/uploads") + fun multiUpload(@PartMap fileMap: Map): Observable + } + + private fun makefileMaps(paths: List): Map { + val map = HashMap() + paths.forEach { + val file = File(it) + val part = makeMultipartBodyPart(it) + map["file; filename=${file.name}"] = part + } + return map + } + + private fun makeMultipartBodyPart(path: String): MultipartBody.Part { + val file = File(path) + //构建requestbody + val requestFile: RequestBody = + file.asRequestBody("multipart/form-data".toMediaTypeOrNull()) + //将resquestbody封装为MultipartBody.Part对象 + return MultipartBody.Part.createFormData("file", file.name, requestFile) + } +} \ No newline at end of file