2

I am Trying to send audio file using Retrofit but ResponseBody always null and Status is 500 internal server error ,I tried a lot of different things but nothing Works

Postman Screenshots:

body

header

My Client:

    public class AudioClient {



    private static Retrofit retrofit = null;


    public static Retrofit getClient(Context context) {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(context.getString(R.string.base_url)).client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

}

addAudioComment method:

@Multipart
@POST("api/Comment/AddSoundComment")
Call<AudioComment> addAudioComment(@Header("Authorization") String contentRange,
                                   @Part("referenceType") RequestBody ReferenceType,
                                   @Part("referenceId") RequestBody ReferenceID,
                                   @Part("parentId") RequestBody ParentID,
                                   @Part  MultipartBody.Part  AudioComment);

The Request :

  File audioFile = new File(mRecordedFilePath);
        RequestBody reqFile = RequestBody.create(MediaType.parse("audio/*"), audioFile);
        audioPart = MultipartBody.Part.createFormData("AudioComment", audioFile.getName(), reqFile);
    Call<AudioComment> apiCall = service.addAudioComment(String.valueOf(SharedPreferencesHelper.getLogInToken(CommentsActivity.this)),
            reqRefType, reqRefId, reqParentId, audioPart);
    //apiCall =service.addAudioComment();
    apiCall.enqueue(new Callback<AudioComment>() {
        @Override
        public void onResponse(Call<AudioComment> call, Response<AudioComment> response) {
            Log.i("RETROFIT", "onResponse Called");

            AudioComment postResult = response.body();

        }

        @Override
        public void onFailure(Call<AudioComment> call, Throwable t) {
            String err = t.getMessage() == null ? "" : t.getMessage();

            showError(R.string.service_failure);
            Log.e("RETROFIT", err);
            setRefreshing(false);
            dismissProgress();
        }
    });
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
Ayat Khrisat
  • 130
  • 2
  • 15

1 Answers1

1

In my case I remove @Multipart from interface and replaced @part with @Body RequestBody requestBody. Eg. as follows,the second parameter is audio file.

public interface APIInterface {

    @POST(url)
    Call<String> postAudioAndGetResponse(@Header("Subscription-Key") String keyValue,
                                         @Body RequestBody requestBody,
                                         @Query("language") String language);
}

and called above method like this

File file = new File(audioFileName);
RequestBody requestBody = RequestBody.create(MediaType.parse("audio/*"), file);

Call<String> str = apiInterface.postAudioAndGetResponse(speechSubscriptionKey, requestBody,"en-IN");

and it worked . Hope it will help someone. :)

Gaurav Pawar
  • 201
  • 3
  • 8