{"id":8063,"date":"2015-11-23T18:33:19","date_gmt":"2015-11-23T18:33:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/23\/download-binary-fie-from-okhttp-open-source-projects-square-okhttp\/"},"modified":"2022-08-30T15:03:02","modified_gmt":"2022-08-30T15:03:02","slug":"download-binary-fie-from-okhttp-open-source-projects-square-okhttp","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/23\/download-binary-fie-from-okhttp-open-source-projects-square-okhttp\/","title":{"rendered":"Download binary fie from OKHTTP-open source projects square\/okhttp"},"content":{"rendered":"<p>I am using OKHTTP client for networking in my android application.<\/p>\n<p>This example shows how to upload binary file. I would like to know how to get inputstream of binary file downloading with OKHTTP client.<\/p>\n<p>Here is the listing of the example :<\/p>\n<pre><code>public class InputStreamRequestBody extends RequestBody {\n\nprivate InputStream inputStream;\nprivate MediaType mediaType;\n\npublic static RequestBody create(final MediaType mediaType, final InputStream inputStream) {\n\n\n    return new InputStreamRequestBody(inputStream, mediaType);\n}\n\nprivate InputStreamRequestBody(InputStream inputStream, MediaType mediaType) {\n    this.inputStream = inputStream;\n    this.mediaType = mediaType;\n}\n\n@Override\npublic MediaType contentType() {\n    return mediaType;\n}\n\n@Override\npublic long contentLength() {\n    try {\n        return inputStream.available();\n    } catch (IOException e) {\n        return 0;\n    }\n}\n\n@Override\npublic void writeTo(BufferedSink sink) throws IOException {\n    Source source = null;\n    try {\n        source = Okio.source(inputStream);\n        sink.writeAll(source);\n    } finally {\n        Util.closeQuietly(source);\n    }\n }\n}\n<\/code><\/pre>\n<p>Current code for simple get request is:<\/p>\n<pre><code>OkHTTPClient client = new OkHttpClient();\nrequest = new Request.Builder().url(\"URL string here\")\n                    .addHeader(\"X-CSRFToken\", csrftoken)\n                    .addHeader(\"Content-Type\", \"application\/json\").build();\n            response = getClient().newCall(request).execute();\n<\/code><\/pre>\n<p>Now how do I convert the response to Inputstream. Something similar to response from Apache HTTP Client like this for OKHTTP response:<\/p>\n<pre><code>InputStream is = response.getEntity().getContent();\n<\/code><\/pre>\n<p>EDIT:<\/p>\n<p>Accepted answer from below. My modified code:<\/p>\n<pre><code>    request = new Request.Builder().url(urlString).build();\n    response = getClient().newCall(request).execute();\n\n    InputStream is = response.body().byteStream();\n\n    BufferedInputStream input = new BufferedInputStream(is);\n    OutputStream output = new FileOutputStream(file);\n\n    byte[] data = new byte[1024];\n\n    long total = 0;\n\n    while ((count = input.read(data)) != -1) {\n        total += count;\n        output.write(data, 0, count);\n    }\n\n    output.flush();\n    output.close();\n    input.close();\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am using OKHTTP client for networking in my android application. This example shows how to upload binary file. I would like to know how to get inputstream of binary file downloading with OKHTTP client. Here is the listing of the example : public class InputStreamRequestBody extends RequestBody { private InputStream inputStream; private MediaType mediaType; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8063","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8063"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8063\/revisions"}],"predecessor-version":[{"id":8655,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8063\/revisions\/8655"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}