[英]How to send File of any type in post parameter in Retrofit 2 Kotlin?
讓我分享一些我已經實現的代碼,以便在請求中發送圖像文件。
以下是我的api請求函數:
@Multipart
@POST("api/order/order_create")
fun createOrder(
@Header("Authorization") authorization: String?,
@Part("category_id") categoryId: RequestBody?,
@Part("size") size: RequestBody?,
@Part("narration") narration: RequestBody?,
@Part("ref_picture") file: RequestBody?
): Call<OrderCreateResponse>
下面是我通過發送必要參數調用api的代碼:
var fbody = RequestBody.create(MediaType.parse("image/*"), imageFile)
var size = RequestBody.create(MediaType.parse("text/plain"), et_custom_order_size.text.toString())
var catId = RequestBody.create(MediaType.parse("text/plain"), selectedID.toString())
var narration = RequestBody.create(MediaType.parse("text/plain"),et_custom_order_narration.text.toString())
val orderCreateAPI = apiService!!.createOrder(complexPreferences?.getPref("token", null), catId,size,narration,fbody)
這里imageFile是通過以下方式獲取的,
imageFile = File(Global.getRealPathFromURI(activity!!, imageUri!!))
使用以下函數獲取真實路徑,
fun getRealPathFromURI(context: Context, contentUri: Uri): String {
var cursor: Cursor? = null
try {
val proj = arrayOf(MediaStore.Images.Media.DATA)
cursor = context.contentResolver.query(contentUri, proj, null, null, null)
val column_index = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(column_index)
} catch (e: Exception) {
Log.e(TAG, "getRealPathFromURI Exception : " + e.toString())
return ""
} finally {
if (cursor != null) {
cursor.close()
}
}
}
通過以上述方式發送圖像,我無法發送它! 請指導我一樣。 提前致謝。
嘗試改變
@Part("ref_picture") file: RequestBody?
至
@Part("ref_picture") file: MultipartBody.Part?
並做到這一點
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)),file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
@Multipart
@POST("register")
Observable<SignInResponse> signUp(@Part("name") RequestBody name, @Part MultipartBody.Part fileToUpload);
然后將圖像文件作為MultipartBody.Part變量傳遞
// image as file
var body: MultipartBody.Part? = null
if (!profileImagePath.isNullOrBlank()) {
val file = File(profileImagePath)
val inputStream = contentResolver.openInputStream(Uri.fromFile(file))
val requestFile = RequestBody.create(MediaType.parse("image/jpeg"), getBytes(inputStream))
body = MultipartBody.Part.createFormData("image", file.name, requestFile)
Log.d("nama file e cuk", file.name)
}
最后你可以使RequestBody變量
RequestBody.create(MediaType.parse("text/plain"), user_full_name)
最后發送請求:)
你可以這樣做:
var propertyImagePart: MultipartBody.Part? = null
imageUrl.value?.let {
val propertyImageFile = File(FILE_PATH)
val propertyImage: RequestBody = RequestBody.create(MediaType.parse("image/*"), propertyImageFile)
propertyImagePart =MultipartBody.Part.createFormData("userImage", propertyImageFile.name, propertyImage)
}
job = launch {
try {
val response = apiServiceWithoutHeader.doUpdateProfile(propertyImagePart,profileRequest.getMultipart()).await()
stateLiveData.postValue(UserProfileState.SuccessUpdateProfile(response))
} catch (e: JsonSyntaxException) {
onException(e)
} catch (e: JsonParseException) {
onException(e)
} catch (e: IOException) {
onException(e)
} catch (e: HttpException) {
onException(e)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.