3

I am new to use Retrofit and I want to send byte array of any file to the server by i always get Failed response from server, and I successfully post file using Volley and HttpUrlConnection both. Now please help me, this is my code snippet .

public class ApiClientPost {

private static final String BASE_URL = "http://BASE.URL/api/";
private static Retrofit retrofit = null;

public static Retrofit getClient(){


    if(retrofit == null){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

public interface ApiInterface {
    @Multipart
    @Headers({
            "content-type: multipart/form-data"
    })
    @POST("eclaims/UploadFiles")
    Call<JsonElement> UploadFiles(@Part MultipartBody.Part body);
}

FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fin);
            DataInputStream dis = new DataInputStream(bis);
            fileContent = toByteArray(dis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        MediaType mediaType = MediaType.parse("video/mp4");
        RequestBody requestFile =
                RequestBody.create(mediaType,
                        file
                );
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("", file.getName(), 
       requestFile);

ApiInterface apiInterface = 
ApiClientPost.getClient().create(ApiInterface.class);
            Call<JsonElement> uploadFile = apiInterface.UploadFiles(body);
            uploadFile.enqueue(new Callback<JsonElement>() {
                @Override
                public void onResponse(Call<JsonElement> call, 
Response<JsonElement> response) {
                    if (response.isSuccessful()) {
                        JsonElement mainResponse = response.body();
                        Log.d("Response ===", mainResponse.toString());
                    } else {
                        Log.e("Response ===", "Failed");
                    }
                }

                @Override
                public void onFailure(Call<JsonElement> call, Throwable t) {
                    Log.e("Failed ===", t.getMessage());
                }
            });

Sorry I am unable to give to URL. It have sensitive data. But i always get failed response from server when i convert a image or video file to byte array and send that byte array to server.

Faheem Raza
  • 189
  • 1
  • 2
  • 9

1 Answers1

4

You don't need to convert it to file, you can pass the byte[] immediately.

public static MultipartBody.Part toMultiPartFile(String name, byte[] byteArray) {
  RequestBody reqFile = RequestBody.create(MediaType.parse("video/mp4"), byteArray);

  return MultipartBody.Part.createFormData(name,
                null, // filename, this is optional
                reqFile);
}
Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40