android - Retrofit @body with @multipart

Android - Retrofit @body with @multipart

When using Retrofit with @Multipart and @Body annotations, you can send a request with a combination of form fields and a part containing a file or binary data. Here's an example:

  1. Define your API interface:
import okhttp3.MultipartBody; import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.http.Multipart; import retrofit2.http.POST; import retrofit2.http.Part; public interface ApiService { @Multipart @POST("your-endpoint") Call<YourResponseType> uploadFileWithFields( @Part("text_field") RequestBody textField, @Part MultipartBody.Part filePart ); } 

In this example, text_field is a form field, and filePart is the part containing the file.

  1. Create a MultipartBody.Part for the file:
import okhttp3.MediaType; import okhttp3.RequestBody; import okhttp3.MultipartBody; // ... // Assuming fileUri is the Uri of the file you want to upload File file = new File(fileUri.getPath()); RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), fileRequestBody); 
  1. Create a RequestBody for the text field:
RequestBody textField = RequestBody.create(MediaType.parse("text/plain"), "Your Text Field Value"); 
  1. Make the API call using the created RequestBody and MultipartBody.Part:
// Create Retrofit instance Retrofit retrofit = new Retrofit.Builder() .baseUrl("your-base-url") .addConverterFactory(GsonConverterFactory.create()) .build(); // Create ApiService instance ApiService apiService = retrofit.create(ApiService.class); // Make the API call Call<YourResponseType> call = apiService.uploadFileWithFields(textField, filePart); // Execute the call asynchronously call.enqueue(new Callback<YourResponseType>() { @Override public void onResponse(Call<YourResponseType> call, Response<YourResponseType> response) { if (response.isSuccessful()) { // Handle success YourResponseType responseData = response.body(); } else { // Handle error // response.errorBody() contains error details } } @Override public void onFailure(Call<YourResponseType> call, Throwable t) { // Handle network errors or request failure } }); 

Replace your-endpoint, YourResponseType, YourRequestBodyType, and the form fields with your actual values. Also, ensure you have the necessary permissions to read the file and handle file-related operations.

This example assumes that you are working with a file upload scenario, but you can modify it based on your specific use case.

Examples

  1. "Retrofit @Body and @Multipart example Android"

    • Code Implementation: Use @Multipart for multipart requests and @Part to define the parts, including the one with @Body.
      @Multipart @POST("upload") Call<ResponseBody> uploadData(@Part("description") RequestBody description, @Part MultipartBody.Part file); 
  2. "Retrofit send JSON and file in the same request"

    • Code Implementation: Combine @Part and @Body to send both JSON data and a file in a single request.
      @Multipart @POST("upload") Call<ResponseBody> uploadData(@Part("json") RequestBody json, @Part MultipartBody.Part file); 
  3. "Retrofit @Body with @Multipart and multiple files"

    • Code Implementation: Send multiple files in a single multipart request with @Part annotations.
      @Multipart @POST("upload") Call<ResponseBody> uploadMultipleFiles(@Part List<MultipartBody.Part> files); 
  4. "Retrofit send image and text data in one request"

    • Code Implementation: Combine @Part annotations to send both image files and text data.
      @Multipart @POST("upload") Call<ResponseBody> uploadData(@Part MultipartBody.Part image, @Part("description") RequestBody description); 
  5. "Retrofit @Multipart request with JSON and file"

    • Code Implementation: Combine @Multipart, @Part, and @Body for sending both JSON and a file in a multipart request.
      @Multipart @POST("upload") Call<ResponseBody> uploadData(@Part MultipartBody.Part file, @Part("json") RequestBody json); 
  6. "Retrofit send text and image using @Multipart"

    • Code Implementation: Use @Multipart and @Part annotations to send both text and image data in the same request.
      @Multipart @POST("upload") Call<ResponseBody> uploadData(@Part("text") RequestBody text, @Part MultipartBody.Part image); 
  7. "Retrofit @Multipart image upload with parameters"

    • Code Implementation: Include additional parameters along with the image in a multipart request using @Part annotations.
      @Multipart @POST("upload") Call<ResponseBody> uploadData(@Part("param1") RequestBody param1, @Part("param2") RequestBody param2, @Part MultipartBody.Part image); 
  8. "Retrofit upload file with progress using @Multipart"

    • Code Implementation: Use @Part annotations along with @Streaming to upload a file with progress tracking.
      @Multipart @POST("upload") Call<ResponseBody> uploadFileWithProgress(@Part("file") MultipartBody.Part file, @Part("description") RequestBody description); 
  9. "Retrofit @Multipart with custom object and file"

    • Code Implementation: Combine @Multipart, @Part, and @Body for sending a custom object and a file in a multipart request.
      @Multipart @POST("upload") Call<ResponseBody> uploadData(@Part MultipartBody.Part file, @Part("customObject") RequestBody customObject); 
  10. "Retrofit @Multipart image upload with headers"

    • Code Implementation: Include custom headers in a multipart image upload request.
      @Multipart @POST("upload") Call<ResponseBody> uploadImageWithHeaders(@Header("Authorization") String token, @Part MultipartBody.Part image); 

More Tags

spring-boot-maven-plugin filesystems android-textinputedittext highlighting android-imagebutton git-clone private asp.net-apicontroller uint underscore.js

More Programming Questions

More Everyday Utility Calculators

More Auto Calculators

More Chemical thermodynamics Calculators

More Geometry Calculators