我在改造中上传了包含其他详细信息的文件,但在成功响应中我得到了状态 0,因为文件是在 bytearray 中发送的。
下面是我的代码:
MyAPI 类:接口和改造实例
@Multipart
@POST("edit_userinfo")
@Headers(## Heading ##
"Content-Type: multipart/form-data",
"Accept: */*"
)
fun editProfile(
@Part("languges") lang: RequestBody,
@Part("user_id") userId: RequestBody,
@Part("name") name: RequestBody,
@Part("gender") gender: RequestBody,
@Part("keyword") keyword: RequestBody,
@Part("mobile") mobile: RequestBody,
@Part image: MultipartBody.Part
): Call<UploadResponse>
return Retrofit.Builder()
.baseUrl("http://phpstack-916737-3182297.cloudwaysapps.com/")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MyAPI::class.java)
MainActivity : api调用
var lang = "English".toRequestBody("text/plain".toMediaTypeOrNull())
var userId = Reques`your text`tBody.create("text/plain".toMediaTypeOrNull(), "1")
var name = RequestBody.create("text/plain".toMediaTypeOrNull(), "Test12")
var gender = RequestBody.create("text/plain".toMediaTypeOrNull(), "male")
var mobno = RequestBody.create("text/plain".toMediaTypeOrNull(), "1212121212")
var keyword = RequestBody.create("text/plain".toMediaTypeOrNull(),"**********")
var image = MultipartBody.Part.createFormData(
"photo",
file.name,
RequestBody.create("image/*".toMediaTypeOrNull(), file)
)
var apiCall = MyAPI().editProfile(
lang, userId, name, gender, keyword, mobno,image
)
apiCall.enqueue(object : Callback<UploadResponse> {
override fun onFailure(call: Call<UploadResponse>, t: Throwable) {
Log.e("TAG", "onFailure: $t")
t.printStackTrace()
layout_root.snackbar(t.message!!)
}
override fun onResponse(
call: Call<UploadResponse>,
response: Response<UploadResponse>
) {
response.body()?.let {
Log.e("TAG", "onResponse: sucess $it")
layout_root.snackbar("Status -> $it")
}
}
})
那么我如何在改造 api 中传递文件。
提前致谢