java 如何在改造中发送字节 [] 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31325723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send byte[] array in retrofit
提问by user2358826
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.
你如何在改造调用中发送 byte[] 数组。我只需要发送字节 []。当我尝试发送改造电话时,我收到此异常。
retrofit.RetrofitError:?retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException:?java.lang.IllegalStateException:? Expected?a?string?but was BEGIN_OBJECT at line?1?column?2
retrofit.RetrofitError:?retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException:?java.lang.IllegalStateException:? 预期的?a?字符串?但在行?1?列?2 处是 BEGIN_OBJECT
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.
我只是将字节数组作为封装在对象类中的 ByteMessage 传递。
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
谢谢迪伦
回答by Igor Mielientiev
For this purpose you can use TypedByteArray
为此,您可以使用 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
"application/octet-stream" - 而不是这个 MIME-TYPE,你可能想使用你的数据格式类型。您可以在此处找到详细信息:http: //www.freeformatter.com/mime-types-list.html
And Spring MVC controller (if you need one):
和 Spring MVC 控制器(如果你需要一个):
@RequestMapping(value = "/send", method = RequestMethod.POST)
public ResponseEntity<String> receive(@RequestBody byte[] data) {
//handle data
return new ResponseEntity<>(HttpStatus.CREATED);
}
回答by Reven
For retrofit2:
对于改造2:
@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
}
});