我正在尝试手动实现自己的分页。而且我和sample repository完全一样。但是我有一些不可理解的错误(在下面的日志中)。我的应用只能加载2-3页并崩溃。
日志:
06-03 09:55:30.457 22190-22423/example.me E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: example.me, PID: 22190
io.reactivex.exceptions.UndeliverableException: java.lang.OutOfMemoryError: Failed to allocate a 168210 byte allocation with 9792 free bytes and 9KB until OOM
at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:64)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 168210 byte allocation with 9792 free bytes and 9KB until OOM
at java.lang.StringFactory.newStringFromBytes(StringFactory.java:79)
at java.lang.StringFactory.newStringFromBytes(StringFactory.java:207)
at okio.Buffer.readString(Buffer.java:631)
at okio.Buffer.readString(Buffer.java:614)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:254)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at com.jakewharton.retrofit2.adapter.rxjava2.CallObservable.subscribeActual(CallObservable.java:41)
at io.reactivex.Observable.subscribe(Observable.java:10838)
at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:10838)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
或有时:
OutOfMemoryError thrown while trying to throw OutOfMemoryError
网络请求代码:
EventApiService eventApiService = RetrofitClient.getApiService();
compositeDisposable.add(eventApiService.getJson(citySlug, FIRST_PAGE_TO_LOAD)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleFirstEventsDataResponse, this::handleFirstEventsDataResponseError));
响应处理代码(第一页):
@Override
public void showData(ResponseData responseData) {
this.events = responseData.getEvents();
this.responseData = responseData;
eventAdapter = new EventAdapter(responseData.getEvents(), eventClickListener);
recyclerView.setAdapter(eventAdapter);
}
响应处理代码(下一页):
@Override
public void showAdditionalData(ResponseData responseData) {
eventAdapter.removeLoadingItem();
isLoading = false;
eventAdapter.addData(responseData.getEvents());
eventAdapter.addLoadingItem();
}
改造建筑方法:
private static Retrofit getRetrofitInstance() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(logging);
OkHttpClient client = builder.build();
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.addConverterFactory(GsonConverterFactory.create())
.build();
}
我尝试做的事情:
android:largeHeap="true"
。它可以让你再加载一页。UPD:
我们发现原因在于图像加载。我添加了fit().centerCrop()
,现在它加载5-7页并崩溃。我的图片加载代码片段在适配器中:
String imageUrl = "";
try {
imageUrl = event.getImages().get(0).getImageUrl();
} catch (NullPointerException e) {
e.printStackTrace();
}
if (eventViewHolder.ivPhoto != null) {
Picasso.get().load(imageUrl).fit().centerCrop().into(eventViewHolder.ivPhoto);
}
新崩溃日志:
06-03 10:32:07.791 17978-17986/example.me E/System: Uncaught exception thrown by finalizer
06-03 10:32:08.438 17978-17986/example.me E/System: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
06-03 10:32:08.439 17978-18715/example.me E/AndroidRuntime: FATAL EXCEPTION: Thread-163055
Process: example.me, PID: 17978
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
答案 0 :(得分:0)
当毕加索尝试设置图像时,视图持有者已经为空。对于调试,您可以使用毕加索独立加载图像并自行设置图像。在设置之前不要忘记检查可空性。
Picasso.with(context)
.load(imageUrl)
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
//check nullability and set image
}
@Override
public void onError() {
}
答案 1 :(得分:0)
使用HttpLoggingInterceptor.Level.BODY时,尝试下载大文件,会将所有正文保存在内存中以备日志。
对OOM来说很容易。
尝试删除日志正文,或者仅记录NONE,basic或header,然后重试。
当您尝试获取大文件时,可以在Retrofit2中使用@Streaming。
@Streaming
@GET
fun downloadFileWithDynamicUrlSync(@Url fileUrl: String): Call<ResponseBody>
尝试在此处删除日志正文。
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
尝试一下。
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.NONE);