Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file in Retrofit 2

enter image description here

I tried the following but on response i am getting 500 error (Internal Server Error) -- help me design the interface for the request in the above screenshot...thanks

@Multipart
@POST("myrecord")
Call<ResponseBody> addRecord(@Query("token") String token,@Query("userid") int userId,
                             @Query("name") String name, @Part("file") RequestBody file);


File file = new File(getRealPathFromURI(data.getData()));
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), getRealPathFromURI(data.getData()));` 
Call<ResponseBody> responseBodyCall = service.addRecord(token, userId,
                "newFileName", requestFile);
        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d("Response", "="+response.code());
                Log.d("Response", "= "+response.message());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("failure", "message = " + t.getMessage());
                Log.d("failure", "cause = " + t.getCause());
            }
        });`
like image 322
Gowsik K C Avatar asked May 05 '16 11:05

Gowsik K C


3 Answers

The following code worked :)

 @Multipart
@POST("myrecord")
Call<ResponseBody> addRecord(@Query("token") String token, @Query("userid") int userId,
                             @Query("name") String name, @Part MultipartBody.Part file);



 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == FILE_SELECT_CODE) && (resultCode == -1)) {

        File file = new File(getRealPathFromURI(data.getData()));

        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), getRealPathFromURI(data.getData()));

        MultipartBody.Part multipartBody =MultipartBody.Part.createFormData("file",file.getName(),requestFile);

        Call<ResponseBody> responseBodyCall = service.addRecord(token, userId, "fileName", multipartBody);
        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d("Success", "success "+response.code());
                Log.d("Success", "success "+response.message());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("failure", "message = " + t.getMessage());
                Log.d("failure", "cause = " + t.getCause());
            }
        });

    }
}
like image 144
Gowsik K C Avatar answered Nov 15 '22 13:11

Gowsik K C


@Multipart
@POST("myrecord")
Call<ResponseBody> addRecord(@Part("file") RequestBody file,@Part MultipartBody.Part file,
@Query("token") String token,@Query("userid") int userId,@Query("name") String name);

RequestBody requestFile =
        RequestBody.create(MediaType.parse("multipart/form-data"), file);

MultipartBody.Part body =
        MultipartBody.Part.createFormData("picture", file.getName(), requestFile);


String descriptionString = "your description";

RequestBody description =
        RequestBody.create(
                MediaType.parse("multipart/form-data"), descriptionString);

for more information look into this link: https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server

like image 38
MobDev Avatar answered Nov 15 '22 15:11

MobDev


If you would like to send file as binary in a body without using multipart you can remove @Multipart annotation from your code and use @Body annotation. It looks like

@POST("myrecord")
Call<ResponseBody> addRecord(@Query("token") String token,@Query("userid") int userId,
                             @Query("name") String name, @Body RequestBody file);


File file = new File(getRealPathFromURI(data.getData()));
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), getRealPathFromURI(data.getData()));
Call<ResponseBody> responseBodyCall = service.addRecord(token, userId,
                "newFileName", requestFile);
        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d("Response", "="+response.code());
                Log.d("Response", "= "+response.message());
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.d("failure", "message = " + t.getMessage());
            Log.d("failure", "cause = " + t.getCause());
        }
    });`
like image 44
Sergei Vasilenko Avatar answered Nov 15 '22 15:11

Sergei Vasilenko