java okhttp3异常_致命异常:java.lang.OutOfMemoryError使用okhttp3 okio下载文件

我有一个应用程序下载来自网络的内容。

音乐,视频,pdf ......就像下载管理器一样。

但现在它每次都在下载内容:

E/LVN/advanced_memory_manager.c: ---------------------------------- AMM report ------------------------------

-> Memory Currently Allocated: 0 bytes <=> 0 components

-> Max Memory Need: 512000 bytes

-> Overall Memory Allocation: 515652 bytes (l:423)

E/art: Throwing OutOfMemoryError "Failed to allocate a 2060 byte allocation with 16777232 free bytes and 308MB until OOM; failed due to fragmentation (required continguous free 4096 bytes where largest contiguous free 0 bytes)"

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher

Process: com.rokki.life2, PID: 32171

java.lang.OutOfMemoryError: Failed to allocate a 2060 byte allocation with 16777232 free bytes and 308MB until OOM; failed due to fragmentation (required continguous free 4096 bytes where largest contiguous free 0 bytes)

at okio.Segment.(Segment.java:58)

at okio.SegmentPool.take(SegmentPool.java:46)

at okio.Buffer.writableSegment(Buffer.java:1114)

at okio.Okio$2.read(Okio.java:137)

at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)

at okio.RealBufferedSource.read(RealBufferedSource.java:50)

at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:381)

at okio.RealBufferedSource.request(RealBufferedSource.java:71)

at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:225)

at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)

at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)

at okhttp3.RealCall.access$100(RealCall.java:30)

at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)

at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at java.lang.Thread.run(Thread.java:818)和

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 106405148 byte allocation with 16777120 free bytes and 82MB until OOM

at java.lang.String.(String.java:332)

at java.lang.String.(String.java:371)

at okio.Buffer.readString(Buffer.java:579)

at okio.Buffer.readString(Buffer.java:562)

at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:244)

at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)

at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)

at okhttp3.RealCall.access$100(RealCall.java:30)

at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)

at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at java.lang.Thread.run(Thread.java:818)我已经将此添加到我的清单:

android:name=".app.MyApp"

android:allowBackup="true"

android:hardwareAccelerated="true"

android:icon="@mipmap/ic_launcher"

tools:replace="android:icon"

android:label="@string/app_name"

android:largeHeap="true"

android:supportsRtl="true"

android:theme="@style/AppTheme.NoActionBar">代码示例:

private OkHttpClient client;

onViewCreated...{

//Initiate OkHttp with interceptor

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

logging.setLevel(HttpLoggingInterceptor.Level.BODY);

client = new OkHttpClient.Builder()

.addInterceptor(logging)

.build();

...

}

private void downloadPdf() {

AsyncTask.execute(new Runnable() {

@Override

public void run() {

client

.newCall(getRequest(Config._API_PDF))

.enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

...

}

@Override

public void onResponse(Call call, Response response) throws IOException {

try {

InputStream ins = response.body().byteStream();

BufferedReader in = new BufferedReader(new InputStreamReader(ins));

String lineTotal = "";

while (true) {

String line = in.readLine();

if (line == null)

break;

else

lineTotal += line;

}

...json parsing ...

...我的BuildGradle:

apply plugin: 'com.android.application'

android {

compileSdkVersion 23

buildToolsVersion "23.0.2"

defaultConfig {

applicationId "com.my.app"

minSdkVersion 17

targetSdkVersion 22

versionCode 1

versionName "1.0"

ndk { abiFilters "armeabi", "x86", "mips" }

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

final SUPPORT_LIBRARY_VERSION = '23.2.0'

final PLAY_SERVICES_VERSION = '8.3.0'

final RETROFIT_VERSION = '2.0.0'

final OKHTTP3_VERSION = '3.2.0'

compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION"

compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"

compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"

compile "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION"

compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"

compile "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"

compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"

compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION"

compile "com.squareup.okhttp3:logging-interceptor:$OKHTTP3_VERSION"

compile 'com.google.code.gson:gson:2.6.2'

compile 'com.jakewharton:butterknife:7.0.1'

}那么我有另一个类似的方法下载视频...

有没有人遇到过这个?

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值