okhttp3大文件下载中的OutOfMemoryError

时间:2017-03-22 11:14:02

标签: android rx-java okhttp3

我尝试通过okhttp3下载zip文件(大小为160 Mb)。几秒钟后应用程序崩溃堆栈:

  

java.lang.IllegalStateException:在Scheduler.Worker线程上抛出致命异常。                                                                在rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59)                                                                at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:428)                                                                在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)                                                                at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607)                                                                在java.lang.Thread.run(Thread.java:761)                                                             引起:java.lang.OutOfMemoryError:无法分配带有1688个空闲字节的8204字节分配和1688B直到OOM                                                                在okio.Segment。(Segment.java:61)                                                                在okio.SegmentPool.take(SegmentPool.java:46)                                                                在okio.Buffer.writableSegment(Buffer.java:1151)                                                                在okio.Okio $ 2.read(Okio.java:136)                                                                在okio.AsyncTimeout $ 2.read(AsyncTimeout.java:238)                                                                at okio.RealBufferedSource.read(RealBufferedSource.java:45)                                                                在okhttp3.internal.http.Http1xStream $ FixedLengthSource.read(Http1xStream.java:377)                                                                在okio.RealBufferedSource.request(RealBufferedSource.java:66)                                                                在okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:238)                                                                在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)                                                                在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)                                                                在okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)                                                                在okhttp3.RealCall.execute(RealCall.java:60)                                                                在com.bvs.data.MapService $ 2.call(MapService.java:102)                                                                在com.bvs.data.MapService $ 2.call(MapService.java:94)                                                                at rx.Observable.unsafeSubscribe(Observable.java:10142)

这是我的代码:

Observable.create((Observable.OnSubscribe<ResponseBody>) subscriber -> {
        Timber.e("Start zip loading");
        Request request = new Request.Builder()
                .url(Tweakables.DEV_API_ENDPOINT + "files/" + Tweakables.MAP_ZIP)
                .build();
        try {
            Response response = client.newCall(request).execute(); // Crash appeared at this line 
            subscriber.onNext(response.body());
            response.close();
        } catch (IOException e) {
            subscriber.onError(e);
        }
    })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<ResponseBody>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(ResponseBody responseBody) {

                }
            });

1 个答案:

答案 0 :(得分:2)

也许对某人有帮助。使用Retrofit注释Streaming

解决了问题
@Streaming
@GET("files/"+Tweakables.MAP_ZIP)
Call<ResponseBody> downloadMap();