[英]How to send a file multipart body with retrofit2?
I have a kotlin app.我有一个 kotlin 应用程序。 I need to send a file to the server with retrofit2.我需要使用retrofit2 将文件发送到服务器。 I have an Intent that I get in onActivityResult
.我有一个 Intent 进入onActivityResult
。 Here is my retrofit这是我的 retrofit
@Multipart
@POST("audio/upload")
fun uploadAudio(
@Part("file") file: MultipartBody.Part,
@Field("title") title: String,
@Field("description") description: String,
@Field("writer") writer: String,
@Field("privacy") privacy: Int,
@Field("duration") duration: Int,
@Field("secretLink") secretLink: String,
@Field("isForKids") isForKids: Boolean,
@Field("language") language: String,
@Field("tags") tags: List<String>,
): Call<Any>
Here is how I use this code:这是我使用此代码的方式:
val file = File(newAudio?.path!!)
val requestFile: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), newAudio?.path!!)
val multipartBody = MultipartBody.Part.createFormData("file", file.name, requestFile)
RetrofitService().uploadAudio(
multipartBody,
title!!,
description!!,
sharedPreferences?.getString("token", null).toString(),
privacy!!,
audioDuration!! / 1000,
secretLink.text.toString(),
forKids!!,
language!!,
tagAdapter.tags
).enqueue(object : Callback<Any> {
@SuppressLint("LogNotTimber")
override fun onFailure(call: Call<Any>, t: Throwable) {
d("#Error", "${t.message}")
mainContent.visibility = VISIBLE
newAudioProgressBar.visibility = GONE
Toast.makeText(requireContext(), t.message, Toast.LENGTH_SHORT).show()
}
@SuppressLint("LogNotTimber")
override fun onResponse(call: Call<Any>, response: Response<Any>) {
navHostFragment.findNavController().navigate(R.id.myAudiosFragment)
Toast.makeText(requireContext(), "Audio uploaded successfully!", Toast.LENGTH_SHORT).show()
}
})
newAudio
equals data.getData()
. newAudio
等于data.getData()
。 My error is: @Part parameters using the MultipartBody.Part must not include a part name in the annotation.
我的错误是: @Part parameters using the MultipartBody.Part must not include a part name in the annotation.
I tried to use PartMap as in other answers but it gives me an error: Type mismatch: inferred type is MultipartBody.Part, but Map<String, RequestBody> was expected
.我尝试像在其他答案中一样使用 PartMap,但它给了我一个错误: Type mismatch: inferred type is MultipartBody.Part, but Map<String, RequestBody> was expected
。 Help me please.请帮帮我。
Meybe you can use with this approach您可以使用这种方法
@POST("audio/upload")
fun uploadAudio(
@Body partFile: RequestBody
): Observable<Response<PhotoProfileResponse>>
And here the requestBody which you pass to Retrofit Intercace这里是您传递给 Retrofit 接口的 requestBody
val file = File(newAudio?.path!!)
val builder = MultipartBody.Builder()
builder.setType(MultipartBody.FORM)
builder.addFormDataPart("title", title!!)
builder.addFormDataPart("description", description!!)
builder.addFormDataPart(
"file", file.name, file
.asRequestBody("multipart/form-data".toMediaTypeOrNull())
)
val requestBody = builder.build()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.