[英]How to parse multi-range request response
I'm trying to send a range request with multiple ranges like the one described here: HTTP/1.1 response to multiple range . 我正在尝试发送具有多个范围的范围请求,例如此处所述: HTTP / 1.1对多个range的响应 。 The problem is that I'm not able to parse the response correctly. 问题是我无法正确解析响应。 How can I parse this using OkHttp? 如何使用OkHttp解析此内容?
I was able to do it by following what's described in this answer Receiving Multipart Response on client side (ClosableHttpResponse) . 通过遵循此答案在客户端接收多部分响应(ClosableHttpResponse)中描述的内容,我能够做到这一点。 I imported the below library: 我导入了以下库:
compile 'com.sun.mail:android-mail:1.5.5' 编译'com.sun.mail:android-mail:1.5.5'
So my code is now like the following: 所以我的代码现在如下所示:
Request request = new Request.Builder()
.url(url)
.addHeader("range", String.format("bytes=%s", TextUtils.join(", ", ranges)))
.build();
Response response = client.newCall(request).execute();
ByteArrayDataSource dataSource = new ByteArrayDataSource(response.body().byteStream(), response.body().contentType().toString());
MimeMultipart multipart = new MimeMultipart(dataSource);
int count = multipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.isMimeType("application/octet-stream")) {
processBinaryStream(bodyPart.getInputStream());
} else {
// Or process different types of data
throw new Exception(String.format("Content type: %s cannot be parsed", bodyPart.getContentType()));
}
}
I'm still not satisfied, having to import this whole mail handling library, just to manage multipart response. 我仍然不满意,不得不导入整个邮件处理库,只是为了管理多部分响应。 But for now, the problem is fixed till I find or come up with a better solution. 但是目前,问题已经解决,直到我找到或提出了更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.