0
我試圖發送範圍請求與多個範圍,如在這裏描述的範圍:HTTP/1.1 response to multiple range。問題是我無法正確解析響應。我怎樣才能解析這個使用OkHttp?如何解析多範圍請求響應
我試圖發送範圍請求與多個範圍,如在這裏描述的範圍:HTTP/1.1 response to multiple range。問題是我無法正確解析響應。我怎樣才能解析這個使用OkHttp?如何解析多範圍請求響應
我能夠做到這一點,按照這個答案Receiving Multipart Response on client side (ClosableHttpResponse)中描述的內容。 我輸入下面的庫:
編譯 'com.sun.mail:機器人郵箱:1.5.5'
所以我的代碼現在是這樣的:
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()));
}
}
我仍然不滿意,不得不導入整個郵件處理庫,只是爲了管理多部分響應。但是現在,問題一直存在,直到我找到或想出更好的解決方案。