我必须在遗留系统和Android设备之间进行双向通信.遗留系统使用小端字节排序.我已经成功实现了接收部分,但发送不起作用.
奇怪,因为对我来说,似乎ByteBuffer类出现故障(我简直不敢相信)
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer = ByteBuffer.allocate(4);
byteBuffer.putInt(88);
byte[] result = byteBuffer.array();
Run Code Online (Sandbox Code Playgroud)
结果:[0,0,0,88]
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer = ByteBuffer.allocate(4);
byteBuffer.putInt(88);
byte[] result = byteBuffer.array();
Run Code Online (Sandbox Code Playgroud)
结果相同:[0,0,0,88]
但是,如果我没有弄错,小端序排序应该返回:[88,0,0,0]
那么我错过了什么呢?
这一点显而易见,但我无法弄清楚.我花了差不多一整天的时间.我很乐意给可以照亮我的人买啤酒.
File file = new File(filePath);
byte[] bytes = new byte[(int)file.length()];
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
dataInputStream.readFully(bytes);
dataInputStream.close();
return new String(bytes);
Run Code Online (Sandbox Code Playgroud)
这是我的代码.我看到字节数组大小不正常,但我无法弄清楚正确的大小.除此之外,内容也不正确.似乎只有文字字符才行.
似乎从二进制文件中获取数据是一种真正的痛苦,我真的很沮丧.
还有一件事:文件内容不是文本,可以是图片,视频或pdf.
我正在为HTTP多部分帖子寻找简单而干净的解决方案,它将发送一些字符串(表单数据)和几个带流媒体支持的文件.(文件需要流式传输以避免内存不足错误)如果可能的话,我想用内置的"org.apache.httpclient"实现这一点.
我能够用HttpURLConnection创建一个干净的解决方案.尽管我对这个解决方案付出了很多努力,但字符串是用8859-x编码而不是UTF-8发送的.
编辑:我的代码在MultiPart和HttpURLConnection源上可用
我用这段代码创建了一个输出流:
HttpURLConnection connection = setupConnection();
dataOutputStream = new DataOutputStream(connection.getOutputStream());
Run Code Online (Sandbox Code Playgroud)
在此之后,我只使用dataOutputStream.writeBytes编写数据
如果我可以从httpclient获得一个输出流,那将是很好的,但它似乎以不同的方式工作.
任何帮助表示赞赏.谢谢
android ×3
java ×2
bytebuffer ×1
endianness ×1
file ×1
httpclient ×1
io ×1
multipart ×1
post ×1
stream ×1