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.
public class ByteMessage {
private byte[] byteArray;
byte[] getByteArray() {
return byteArray;
}
setByteArray(byte[] bytes){
byteArray = bytes;
}
}
@POST("/send")
sendBytes(ByteMesssage msg);
Server side:
sendBytes(ByteMessage msg) {
byte[] byteArray = msg.getByte();
...doSomething...
}
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
Annotation Type FormUrlEncodedDenotes that the request body will use form URL encoding. Fields should be declared as parameters and annotated with @Field . Requests made with this annotation will have application/x-www-form-urlencoded MIME type.
What is Retrofit. Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.
Retrofit is a type-safe REST client for Android, Java and Kotlin developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp.
For this purpose you can use TypedByteArray
Your Retrofit service will look like this:
@POST("/send")
void upload(@Body TypedInput bytes, Callback<String> cb);
Your client code:
byte[] byteArray = ...
TypedInput typedBytes = new TypedByteArray("application/octet-stream", byteArray);
remoteService.upload(typedBytes, new Callback<String>() {
@Override
public void success(String s, Response response) {
//Success Handling
}
@Override
public void failure(RetrofitError retrofitError) {
//Error Handling
}
});
"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):
@RequestMapping(value = "/send", method = RequestMethod.POST)
public ResponseEntity<String> receive(@RequestBody byte[] data) {
//handle data
return new ResponseEntity<>(HttpStatus.CREATED);
}
For retrofit2:
@POST("/send")
void upload(@Body RequestBody bytes, Callback<String> cb);
usage:
byte[] params = ...
RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"), params);
remoteService.upload(body, new Callback<String>() {
@Override
public void success(String s, Response response) {
//Success Handling
}
@Override
public void failure(RetrofitError retrofitError) {
//Error Handling
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With