0

I try to make a multipart request with retrofit 2.0 to upload an image to my server.

For now I have RequestBody with my image

RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);

Then I create from data in this way

imagenPerfil = MultipartBody.Part.createFormData("user[image]",file.getName(),requestFile);

My retrofit interface

@Multipart
@Headers({"Accept: application/json","X-OS:android","X-Api-Version:1","X-FIREBASE-TOKEN:token"})
@POST("/update_user_details")
Call<UserDetailAddResponse> uploadMulFile(@Header("X-User-Token") String token_header, @Header("X-User-Email") String email,
                                          @PartMap Map<String,Map<String,Object>> object,
                                          @Part MultipartBody.Part image);

request taking too much time to go on server or in the end it stop or don't send request on server..what i have to do would i use this in another Thread or is it okay ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rahul Bh
  • 123
  • 2
  • 15

1 Answers1

2

You can increase the timeouts in the Retrofit settings.

OkHttpClient okHttpClient = new OkHttpClient.Builder()  
    .connectTimeout(1, TimeUnit.MINUTES)
    .readTimeout(30, TimeUnit.SECONDS)
    .writeTimeout(15, TimeUnit.SECONDS)
    .build();

Retrofit.Builder builder = new Retrofit.Builder()  
    .baseUrl("http://10.0.2.2:3000/")
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create());

Maybe it help for you!

More: https://futurestud.io/tutorials/retrofit-2-customize-network-timeouts

  • Sorry @ILLIADEREVIANKO for late reply .......i got stuck in another problem can u plzz tell one thing about retrofit it possible tu send this json to server{ “user” :{ “name” : “helo”, “image” : “”, language_id” : 1, “relation_id”: 2 } } – Rahul Bh Oct 25 '18 at 06:32
  • @RahulChaudhary can I help you more? – ILLIA DEREVIANKO Oct 25 '18 at 08:47
  • Thankyou @ILLIA DEREVIANKO but the problem is solved.....if i stuck anywhere i'll definatrly ask you :D – Rahul Bh Oct 26 '18 at 05:36
  • @ILLIADEREVIANKO, what about a retrofilt multipart request of a large image, where server process its size, nudity check and s3 upload and respond back with url strings. Are you proposing these timeouts are optimal ? – binrebin May 09 '20 at 19:00
  • @binrebin of course no. It's a flexible configuration and you should adapt it according to your requirements – ILLIA DEREVIANKO May 10 '20 at 12:56