How do I parse the form data that comes with Retrofit?

Viewed 50

How can I parse the following response value? It contains both .wav file and json data. The service gives two content types. Somebody Content-Type: application/json; charset=utf-8 Content-Disposition: form-data; name=Json the other Content-Type: audio/wav Content-Disposition: form-data; filename=AudioFromTts.wav; name=AudioFromTts Content-Encoding: binary

How can I parse them? I need to play the audio file. I need to show Text in Json data on screen

--379bfc09-27b4-4a5a-a85a-e699f5129379
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name=Json

{
   "type":"message",
   "id":"9abcba9b-5a19-4020-8ac2-1ec940857768",
   "timestamp":"2022-01-10T12:33:44.6423693+00:00",
   "textFormat":null,
   "speak":"<speak>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</speak>"
}

--379bfc09-27b4-4a5a-a85a-e699f5129379
Content-Type: audio/wav
Content-Disposition: form-data; filename=AudioFromTts.wav; name=AudioFromTts
Content-Encoding: binary

RIFF4WAVEfmt �>}data�3
  
!"%,&'..5@A??B5@88999699BB69299=26+>B.299"2'6'"2''""2.

--379bfc09-27b4-4a5a-a85a-e699f5129379--
1 Answers

Showing the wav part as the json section is pretty straight forward.

To decode the wav file you need to create an interface like so . @Streaming would let retrofit know that it is streaming data that is being processed

//On your api interface
@POST("path/to/your/resource")
@Streaming
void apiRequest(Callback<POJO> callback);

restAdapter.apiRequest(new Callback<POJO>() {
        @Override
        public void success(POJO pojo, Response response) {
            try {
                //you can now get your file in the InputStream
                InputStream is = response.getBody().in();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });
Related