0

I'm trying to send a range request with multiple ranges like the one described here: HTTP/1.1 response to multiple range. The problem is that I'm not able to parse the response correctly. How can I parse this using OkHttp?

Community
  • 1
  • 1
Sohayb Hassoun
  • 657
  • 7
  • 17

1 Answers1

0

I was able to do it by following what's described in this answer Receiving Multipart Response on client side (ClosableHttpResponse). I imported the below library:

compile '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.

Community
  • 1
  • 1
Sohayb Hassoun
  • 657
  • 7
  • 17