封装心跳和文件上传API
parent
89db2e60c4
commit
8500eaf385
@ -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<HeartbeatApi.Api>(Api::class.java) {
|
||||||
|
|
||||||
|
open fun heartBeat(sn: String) {
|
||||||
|
observe(api.heartBeat(sn), object : RequestNoResultCallBack<BaseResponse>() {
|
||||||
|
override fun onError(error: String?) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onComplete() {
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface Api {
|
||||||
|
@GET("xx/xxx")
|
||||||
|
@FormUrlEncoded
|
||||||
|
fun heartBeat(@Query("sn") sn: String): Observable<BaseResponse>
|
||||||
|
}
|
||||||
|
}
|
@ -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<UploadFileApi.Api>(Api::class.java) {
|
||||||
|
|
||||||
|
open fun singleUpload(path: String) {
|
||||||
|
observe(
|
||||||
|
api.singleUpload(makeMultipartBodyPart(path)),
|
||||||
|
object : RequestResultCallBack<BaseResponse>() {
|
||||||
|
override fun onResult(result: BaseResponse) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onError(error: String?) {
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun multiUpload(paths: List<String>) {
|
||||||
|
observe(
|
||||||
|
api.multiUpload(makefileMaps(paths)),
|
||||||
|
object : RequestResultCallBack<BaseResponse>() {
|
||||||
|
override fun onResult(result: BaseResponse) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onError(error: String?) {
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface Api {
|
||||||
|
@Multipart
|
||||||
|
@POST("xxx/upload")
|
||||||
|
fun singleUpload(@Part file: MultipartBody.Part?): Observable<BaseResponse>
|
||||||
|
|
||||||
|
@Multipart
|
||||||
|
@POST("xxx/uploads")
|
||||||
|
fun multiUpload(@PartMap fileMap: Map<String, MultipartBody.Part>): Observable<BaseResponse>
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun makefileMaps(paths: List<String>): Map<String, MultipartBody.Part> {
|
||||||
|
val map = HashMap<String, MultipartBody.Part>()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue