Skip to content
Advertisement

How to send byte[] array in retrofit

How do you send byte[] array in retrofit call. I just need to send over byte[]. I get this exception when I have been trying to send a retrofit call.

retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:  Expected a string but was BEGIN_OBJECT at line 1 column 2

What is the way I can make the call using retrofit.

I was simply passing byte array as a ByteMessage encapsulated in the object class.

JavaScript
JavaScript

Server side:

JavaScript

I could not find resources on the stack overflow or googling for a proper solution passing byte arrays through retrofit call.

Can anyone please help with this.

Thanks Dhiren

Advertisement

Answer

For this purpose you can use TypedByteArray

Your Retrofit service will look like this:

JavaScript

Your client code:

JavaScript

“application/octet-stream” – instead this MIME-TYPE, you maybe want to use your data format type. Detail information you can find here: http://www.freeformatter.com/mime-types-list.html

And Spring MVC controller (if you need one):

JavaScript
Advertisement