内存不足使用Retrofit 2下载文件

时间:2015-11-12 00:12:21

标签: java android retrofit

我有一个pdf查看器应用程序,我必须下载大的pdf文件(例如136mb)我正在使用retrofit2-beta2进行此过程。问题是我总是耗尽内存。我如何告诉改装我将下载一个大文件,请给我一个byteStream?

我的界面是:

@GET("url")
Call<ResponseBody> getData(params);

我有一个ProgressResponseBody类,它正在扩展ResponseBody 我在这里设置progressListener,以便能够刷新我的进度条,

并且在onResponse函数中我只得到像

这样的InputStream
InputStream input = response.body().byteStream();
FileOutputStream out = new FileOutputStream(file);
int bufferSize=1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while (len = input.read(buffer) != -1) {
    out.write(buffer,0,len);
} 
if(out!=null)
    out.close();

更新

我已将@Stream添加到接口但现在我在ProgressResponseBody.java中获得了NetworkOnMainThreadException 在super.read(sink,byteCount)中抛出错误;行。我怎么能把它放到一个单独的线程?

@Override
    public BufferedSource source() throws IOException {
        if (bufferedSource == null) {
            bufferedSource = Okio.buffer(source(responseBody.source()));
        }
        return bufferedSource;
    }

    private Source source(Source source) {
        return new ForwardingSource(source) {
            long totalBytesRead = 0L;

            @Override
            public long read(Buffer sink, long byteCount) throws IOException {
                long bytesRead = super.read(sink, byteCount);
                totalBytesRead += bytesRead != -1 ? bytesRead : 0;
                progressListener.update(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
                return bytesRead;
            }
        };
    }

1 个答案:

答案 0 :(得分:4)

@Streaming注释添加到您的调用中以获取原始Please create a token that has 'write:public_key' scope Enter Token: 3310b4ef5d0dbbb8687b992e6f78e02cd34e4d6d ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7zwo/h3mVL2RwOyMeUyGZblLT+nZy9pS6akUMSiOaVkphCdQzoHH/6xbDPiTKNlXC+1JYmQq4DaeglFdVc+FQHp3ZlbhZp6YPemXHnGtKamtQrJHS+HA7zE+F3xPzLQlVTgUdlE4ckU0THRK7WdwXa1sSMO2UNBrn7MGZ4wskU4PO8QFBjFHN0e5SIv2mlF/Oi1FoSRHIEohHAY6JSE8lnxkYFA5XeVkygLFQbZ7dKpVaZg4Y1LvrZUnThmI+e1QxP9ttUj5Ike6wFewBTvTjtPb8hkEHp3GuwjmwN9SKIW4d5bmrJtGxQjYmY4PHhW5DnySBQWBeOnQxhqVQFqlN &{POST http://github.com/api/v3/user/keys HTTP/1.1 1 1 map[Authorization:[token 3310b4ef5d0dbbb8687b992e6f78e02cd34e4d6d] Content-Type:[application/json]] {{"Key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7zwo/h3mVL2RwOyMeUyGZblLT+nZy9pS6akUMSiOaVkphCdQzoHH/6xbDPiTKNlXC+1JYmQq4DaeglFdVc+FQHp3ZlbhZp6YPemXHnGtKamtQrJHS+HA7zE+F3xPzLQlVTgUdlE4ckU0THRK7WdwXa1sSMO2UNBrn7MGZ4wskU4PO8QFBjFHN0e5SIv2mlF/Oi1FoSRHIEohHAY6JSE8lnxkYFA5XeVkygLFQbZ7dKpVaZg4Y1LvrZUnThmI+e1QxP9ttUj5Ike6wFewBTvTjtPb8hkEHp3GuwjmwN9SKIW4d5bmrJtGxQjYmY4PHhW5DnySBQWBeOnQxhqVQFqlN","Title":"dixon@homedesktop"}} 418 [] false github.com map[] map[] <nil> map[] <nil> <nil>} Status 422 Body: {"message":"Validation Failed","documentation_url":"https://developer.github.com/v3/users/keys/#create-a-public-key","errors":[{"resource":"PublicKey","code":"custom","field":"key","message":"key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key"}]}

ResponseBody