From 7f3560aa5c2db62788de96f718d66c26521ed13b Mon Sep 17 00:00:00 2001 From: xiaoyan Date: Wed, 4 Jan 2023 09:46:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 +- .../com/navinfo/volvo/http/DefaultResponse.kt | 7 ++++ .../navinfo/volvo/http/NavinfoVolvoCall.kt | 21 ++++++---- .../navinfo/volvo/http/NavinfoVolvoService.kt | 7 +++- .../volvo/ui/message/ObtainMessageFragment.kt | 14 +++++-- .../ui/message/ObtainMessageViewModel.kt | 39 ++++++++++++++++--- 6 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/com/navinfo/volvo/http/DefaultResponse.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d19fd34..2e004a7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,7 +26,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.NavinfoVolvo" - tools:targetApi="31"> + android:usesCleartextTraffic="true"> { + var code: Int = 0 + var message: String = "" + var data: T? = null +} \ No newline at end of file diff --git a/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoCall.kt b/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoCall.kt index 97d342b..3e1bf0c 100644 --- a/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoCall.kt +++ b/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoCall.kt @@ -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 + } } - - } \ No newline at end of file diff --git a/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoService.kt b/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoService.kt index 52ea7ca..e4ea112 100644 --- a/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoService.kt +++ b/app/src/main/java/com/navinfo/volvo/http/NavinfoVolvoService.kt @@ -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) @POST("/img/upload") @Multipart - fun uploadAttachment(@Part("picture") attachmentFile: File) + suspend fun uploadAttachment(@Part attachmentFile: MultipartBody.Part):DefaultResponse> @POST("/img/download") - fun downLoadAttachment(@Body downloadData: MutableMap) + fun downLoadAttachment(@Body downloadData: MutableMap):Call>> } \ No newline at end of file diff --git a/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageFragment.kt b/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageFragment.kt index b1cb9c9..97bb741 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageFragment.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageFragment.kt @@ -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("/") diff --git a/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageViewModel.kt b/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageViewModel.kt index 795ef28..4fb4456 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageViewModel.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/message/ObtainMessageViewModel.kt @@ -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 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) + } + } + } } \ No newline at end of file