feat: 增加上传文件接口调用

This commit is contained in:
xiaoyan 2023-01-04 09:46:02 +08:00
parent c95096d4fd
commit 7f3560aa5c
6 changed files with 70 additions and 20 deletions

View File

@ -26,7 +26,7 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.NavinfoVolvo" android:theme="@style/Theme.NavinfoVolvo"
tools:targetApi="31"> android:usesCleartextTraffic="true">
<activity <activity
android:name=".ui.message.MessageActivity" android:name=".ui.message.MessageActivity"
android:exported="false" android:exported="false"

View File

@ -0,0 +1,7 @@
package com.navinfo.volvo.http
class DefaultResponse<T> {
var code: Int = 0
var message: String = ""
var data: T? = null
}

View File

@ -1,15 +1,20 @@
package com.navinfo.volvo.http package com.navinfo.volvo.http
import com.navinfo.volvo.db.dao.entity.Attachment
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.io.File
class NavinfoVolvoCall { class NavinfoVolvoCall {
private val retrofit by lazy { companion object {
private val service by lazy {
Retrofit.Builder().baseUrl("http://ec2-52-81-73-5.cn-north-1.compute.amazonaws.com.cn:8088/") Retrofit.Builder().baseUrl("http://ec2-52-81-73-5.cn-north-1.compute.amazonaws.com.cn:8088/")
.addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
.build() .build()
.create(NavinfoVolvoService::class.java)
} }
companion object {
private var instance: NavinfoVolvoCall? = null private var instance: NavinfoVolvoCall? = null
get() { get() {
if (field == null) { if (field == null) {
@ -17,7 +22,9 @@ class NavinfoVolvoCall {
} }
return field return field
} }
}
fun getApi(): NavinfoVolvoService {
return service
}
}
} }

View File

@ -1,5 +1,8 @@
package com.navinfo.volvo.http package com.navinfo.volvo.http
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.http.Body import retrofit2.http.Body
import retrofit2.http.Multipart import retrofit2.http.Multipart
import retrofit2.http.POST import retrofit2.http.POST
@ -17,7 +20,7 @@ interface NavinfoVolvoService {
fun deleteCardByApp(@Body deleteData: MutableMap<String, String>) fun deleteCardByApp(@Body deleteData: MutableMap<String, String>)
@POST("/img/upload") @POST("/img/upload")
@Multipart @Multipart
fun uploadAttachment(@Part("picture") attachmentFile: File) suspend fun uploadAttachment(@Part attachmentFile: MultipartBody.Part):DefaultResponse<MutableMap<String, String>>
@POST("/img/download") @POST("/img/download")
fun downLoadAttachment(@Body downloadData: MutableMap<String, String>) fun downLoadAttachment(@Body downloadData: MutableMap<String, String>):Call<DefaultResponse<MutableMap<String, String>>>
} }

View File

@ -16,6 +16,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation import androidx.navigation.Navigation
import com.bumptech.glide.Glide
import com.easytools.tools.DateUtils import com.easytools.tools.DateUtils
import com.easytools.tools.ToastUtils import com.easytools.tools.ToastUtils
import com.elvishew.xlog.XLog import com.elvishew.xlog.XLog
@ -80,13 +81,18 @@ class ObtainMessageFragment: Fragment() {
// 展示照片文件或录音文件 // 展示照片文件或录音文件
for (attachment in it.attachment) { for (attachment in it.attachment) {
if (attachment.attachmentType == AttachmentType.PIC) { if (attachment.attachmentType == AttachmentType.PIC) {
// Glide.with(context!!) Glide.with(context!!)
// .asBitmap().fitCenter() .asBitmap().fitCenter()
// .load(attachment.pathUrl) .load(attachment.pathUrl)
// .into(binding.imgMessageAttachment) .into(binding.imgMessageAttachment)
// 显示名称 // 显示名称
binding.tvPhotoName.text = attachment.pathUrl.replace("\\", "/").substringAfterLast("/") binding.tvPhotoName.text = attachment.pathUrl.replace("\\", "/").substringAfterLast("/")
hasPhoto = true hasPhoto = true
// 如果当前attachment文件是本地文件开始尝试网络上传
if (!attachment.pathUrl.startsWith("http")) {
obtainMessageViewModel.uploadAttachment(File(attachment.pathUrl))
}
} }
if (attachment.attachmentType == AttachmentType.AUDIO) { if (attachment.attachmentType == AttachmentType.AUDIO) {
binding.tvAudioName.text = attachment.pathUrl.replace("\\", "/").substringAfterLast("/") binding.tvAudioName.text = attachment.pathUrl.replace("\\", "/").substringAfterLast("/")

View File

@ -1,13 +1,19 @@
package com.navinfo.volvo.ui.message package com.navinfo.volvo.ui.message
import androidx.lifecycle.LiveData import androidx.lifecycle.*
import androidx.lifecycle.MutableLiveData import com.easytools.tools.ToastUtils
import androidx.lifecycle.ViewModel import com.elvishew.xlog.XLog
import androidx.lifecycle.liveData
import com.navinfo.volvo.db.dao.entity.Attachment 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 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() { class ObtainMessageViewModel: ViewModel() {
private val msgLiveData: MutableLiveData<Message> by lazy { private val msgLiveData: MutableLiveData<Message> by lazy {
@ -84,4 +90,25 @@ class ObtainMessageViewModel: ViewModel() {
this.msgLiveData.value?.sendDate = sendTime this.msgLiveData.value?.sendDate = sendTime
this.msgLiveData.postValue(this.msgLiveData.value) 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)
}
}
}
} }