feat: 增加上传文件接口调用
This commit is contained in:
parent
c95096d4fd
commit
7f3560aa5c
@ -26,7 +26,7 @@
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.NavinfoVolvo"
|
||||
tools:targetApi="31">
|
||||
android:usesCleartextTraffic="true">
|
||||
<activity
|
||||
android:name=".ui.message.MessageActivity"
|
||||
android:exported="false"
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.navinfo.volvo.http
|
||||
|
||||
class DefaultResponse<T> {
|
||||
var code: Int = 0
|
||||
var message: String = ""
|
||||
var data: T? = null
|
||||
}
|
@ -1,15 +1,20 @@
|
||||
package com.navinfo.volvo.http
|
||||
|
||||
import com.navinfo.volvo.db.dao.entity.Attachment
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.create
|
||||
import java.io.File
|
||||
|
||||
class NavinfoVolvoCall {
|
||||
private val retrofit by lazy {
|
||||
Retrofit.Builder().baseUrl("http://ec2-52-81-73-5.cn-north-1.compute.amazonaws.com.cn:8088/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
}
|
||||
companion object {
|
||||
private val service by lazy {
|
||||
Retrofit.Builder().baseUrl("http://ec2-52-81-73-5.cn-north-1.compute.amazonaws.com.cn:8088/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create(NavinfoVolvoService::class.java)
|
||||
}
|
||||
|
||||
private var instance: NavinfoVolvoCall? = null
|
||||
get() {
|
||||
if (field == null) {
|
||||
@ -17,7 +22,9 @@ class NavinfoVolvoCall {
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
fun getApi(): NavinfoVolvoService {
|
||||
return service
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
package com.navinfo.volvo.http
|
||||
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.Multipart
|
||||
import retrofit2.http.POST
|
||||
@ -17,7 +20,7 @@ interface NavinfoVolvoService {
|
||||
fun deleteCardByApp(@Body deleteData: MutableMap<String, String>)
|
||||
@POST("/img/upload")
|
||||
@Multipart
|
||||
fun uploadAttachment(@Part("picture") attachmentFile: File)
|
||||
suspend fun uploadAttachment(@Part attachmentFile: MultipartBody.Part):DefaultResponse<MutableMap<String, String>>
|
||||
@POST("/img/download")
|
||||
fun downLoadAttachment(@Body downloadData: MutableMap<String, String>)
|
||||
fun downLoadAttachment(@Body downloadData: MutableMap<String, String>):Call<DefaultResponse<MutableMap<String, String>>>
|
||||
}
|
@ -16,6 +16,7 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.navigation.Navigation
|
||||
import com.bumptech.glide.Glide
|
||||
import com.easytools.tools.DateUtils
|
||||
import com.easytools.tools.ToastUtils
|
||||
import com.elvishew.xlog.XLog
|
||||
@ -80,13 +81,18 @@ class ObtainMessageFragment: Fragment() {
|
||||
// 展示照片文件或录音文件
|
||||
for (attachment in it.attachment) {
|
||||
if (attachment.attachmentType == AttachmentType.PIC) {
|
||||
// Glide.with(context!!)
|
||||
// .asBitmap().fitCenter()
|
||||
// .load(attachment.pathUrl)
|
||||
// .into(binding.imgMessageAttachment)
|
||||
Glide.with(context!!)
|
||||
.asBitmap().fitCenter()
|
||||
.load(attachment.pathUrl)
|
||||
.into(binding.imgMessageAttachment)
|
||||
// 显示名称
|
||||
binding.tvPhotoName.text = attachment.pathUrl.replace("\\", "/").substringAfterLast("/")
|
||||
hasPhoto = true
|
||||
|
||||
// 如果当前attachment文件是本地文件,开始尝试网络上传
|
||||
if (!attachment.pathUrl.startsWith("http")) {
|
||||
obtainMessageViewModel.uploadAttachment(File(attachment.pathUrl))
|
||||
}
|
||||
}
|
||||
if (attachment.attachmentType == AttachmentType.AUDIO) {
|
||||
binding.tvAudioName.text = attachment.pathUrl.replace("\\", "/").substringAfterLast("/")
|
||||
|
@ -1,13 +1,19 @@
|
||||
package com.navinfo.volvo.ui.message
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.liveData
|
||||
import androidx.lifecycle.*
|
||||
import com.easytools.tools.ToastUtils
|
||||
import com.elvishew.xlog.XLog
|
||||
import com.navinfo.volvo.db.dao.entity.Attachment
|
||||
import com.navinfo.volvo.db.dao.entity.Message
|
||||
import com.navinfo.volvo.db.dao.entity.AttachmentType
|
||||
import java.util.UUID
|
||||
import com.navinfo.volvo.db.dao.entity.Message
|
||||
import com.navinfo.volvo.http.NavinfoVolvoCall
|
||||
import kotlinx.coroutines.launch
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
|
||||
class ObtainMessageViewModel: ViewModel() {
|
||||
private val msgLiveData: MutableLiveData<Message> by lazy {
|
||||
@ -84,4 +90,25 @@ class ObtainMessageViewModel: ViewModel() {
|
||||
this.msgLiveData.value?.sendDate = sendTime
|
||||
this.msgLiveData.postValue(this.msgLiveData.value)
|
||||
}
|
||||
|
||||
fun uploadAttachment(attachmentFile: File) {
|
||||
// 启用协程调用网络请求
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val requestFile: RequestBody =
|
||||
RequestBody.create(MediaType.parse("multipart/form-data"), attachmentFile)
|
||||
val body = MultipartBody.Part.createFormData("picture", attachmentFile.getName(), requestFile)
|
||||
val result = NavinfoVolvoCall.getApi().uploadAttachment(body)
|
||||
XLog.d(result.code)
|
||||
if (result.code == 200) { // 请求成功
|
||||
// 获取上传后的结果
|
||||
} else {
|
||||
ToastUtils.showToast(result.message)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
ToastUtils.showToast(e.message)
|
||||
XLog.d(e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user