0

I'm trying to upload an image to my database using Retrofit but had no luck so far. After reading all the threads related to this and trying a lot of solutions I've decided to post my problem. So this is my code in Android

API

@Multipart
@POST("createevent")
Call<JsonResponse> createEvent(@Field("title") String title,
                               @Part MultipartBody.Part imageFile,
                               @Field("description") String description,
                               @Field("id_type") int id_type,
                               @Part("image") RequestBody image,
                               @Field("id_group[1]") int id_group,
                               @Header("Authorization")String authHeader);

REQUEST

public void CreateEvent(){

    keepAllDates();
    File file = new File("/storage/emulated/0/Download/carmena.jpg");
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("image", file.getName(), requestFile);
    RequestBody image =
            RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name");


    Call<JsonResponse> peticion = api.createEvent (eventTile,body,eventDescription, idTypeEvent,image,id_group,tokenHc);
    peticion.enqueue(new Callback<JsonResponse>() {
        @Override
        public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {

            int code = response.body().getCode();
            JsonResponse json = response.body();
            Log.d ( "Respuesta del servidor", response.body ().getMessage () );

            switch (code) {
                case 200:
                    String message = response.body ().getMessage ();

                    //listener.onGetEventsFinish ();

                    break;
                case 400:
                    // Toast.makeText ( MainActivity.this, errorMessage, Toast.LENGTH_SHORT ).show ();
                    String errorMessage = response.body ().getMessage ();
                    break;

                default:
                    //Toast.makeText ( MainActivity.this, errorMessage, Toast.LENGTH_SHORT ).show ();
                    String defaultmsg = response.body ().getMessage ();

            }

        }
        @Override
        public void onFailure(Call<JsonResponse> call, Throwable t) {

            Log.d ("Failure message", "fail");
            Log.d ("fail is", String.valueOf(t));

        }

    });
}

POSTMAN

Postman request with all fields

I can't get it to work, it just doesn't upload anything.

  • Possible duplicate of [POST Multipart Form Data using Retrofit 2.0 including image](https://stackoverflow.com/questions/34562950/post-multipart-form-data-using-retrofit-2-0-including-image) – Vishal G. Gohel Mar 12 '19 at 12:39
  • '@'Field parameters won't work, when you are sending '@'Multipart data with retrofit2. – Csisanyi Mar 12 '19 at 14:30

1 Answers1

0

If you want to send an image with a request along with other form data (like in the image you attached), the easiest way of doing this with Retrofit 2.0 is by passing it in a PartMap. This link has an excellent tutorial on how to do it, it's basically just a Map object with key-value pairs.

  • Nice tutorial, it might solve my problem, thanks! One problem I've encountered while trying to implement it, how would you pass a header? Because in my app i need to pass a token as an authorization through header. Does @Header work in this? – Miguel Heredia Mar 13 '19 at 10:38
  • Yes, @Header still works, you can add it in the method parameters like you used to. You can also use method annotations to set headers if you want to set things beforehand such as request type. – Dimitar Stoyanov Jun 11 '19 at 15:46