Flush previous Retrofit responses

六月ゝ 毕业季﹏ 提交于 2019-12-08 14:20:12

问题


In my Android app I need to download metadata for some files from a XML API. I am using RetroFit to handle the requests that I've implemented as asynchronous ones, in the Callback I parse the XML and save in a file the data I need.

When I need to get info for many files, I get OutOfMemoryError and analyzing the heap with MAT, I discovered that XML body of previous responses is still there. Is there any way to force the app to flush the already used XML?


回答1:


I think I found a solution:

By default, the OkHttpClient uses a memory cache implementation for HttpResponseCache, switching to a disk implementation I was able to run 10000 requests without getting any OutOfMemory error.

I have looked at this example and edited the following lines to use Android internal storage:

File cacheDir = new File(System.getProperty("java.io.tmpdir"), "okhttp-cache");
cache = new HttpResponseCache(cacheDir, 10L * 1024 * 1024);
okHttpClient = new OkHttpClient();
okHttpClient.setResponseCache(cache);


来源:https://stackoverflow.com/questions/24930700/flush-previous-retrofit-responses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!