通知私はアクティビティである自分のコンテキストにWeakReferenceを使用しています。しかし、私はコンテキストを直接使用すべきですか? (WeakReferenceなし)?私はそれを私のcreateSchoolSearchObservableメソッドへのパラメタとして渡すべきですか?FIRST Rxjava2とRetrofitでこのエラーが発生するのはなぜですか?私は弱い参照やアクティビティを直接使うべきですか?
public void startSearchFeature(Context context) {
WeakReference<SchoolSearchActivity> activityReference = new WeakReference<>((SchoolSearchActivity) context);
Disposable disposable = createSchoolSearchObservable(context)
.debounce(400, TimeUnit.MILLISECONDS)
.filter(query -> query.length() >= 2)
.distinctUntilChanged()
.switchMap(new Function<String, ObservableSource<SchoolResponse>>() {
@Override
public ObservableSource<SchoolResponse> apply(String query) throws Exception {
return loadSchools(query);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(schoolSearchResponse -> {
List<Node<SchoolSearchResult>> results = schoolSearchResponse.getSchoolSearchResults();
if (results == null) {
Log.e(TAG, "Results are null");
}
if (activityReference.get() != null) {
ListAdapter adapter = activityReference.get().binding.schoolAutocompleteSearch.getAdapter();
if (adapter instanceof SchoolSearchAdapter) {
((SchoolSearchAdapter) adapter).setSchoolSearchResults(results);
} else {
activityReference.get()
.binding.schoolAutocompleteSearch
.setAdapter(SchoolSearchAdapter.newInstance(activityReference.get(), R.layout.school_search_item, results));
}
}
}, throwable -> {
Log.e(TAG, throwable.getMessage(), throwable);
});
addDisposable(disposable);
}
// https://github.com/amitshekhariitbhu/RxJava2-Android-Samples/blob/master/app/src/main/java/com/rxjava2/android/samples/ui/search/SearchActivity.java
private Observable<String> createSchoolSearchObservable(Context context) {
WeakReference<SchoolSearchActivity> activityReference = new WeakReference<>((SchoolSearchActivity) context);
final PublishSubject<String> subject = PublishSubject.create();
final TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence query, int i, int i1, int i2) {
Log.d(TAG, query.toString());
subject.onNext(query.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
};
final AdapterView.OnItemClickListener listener = (AdapterView<?> parent, View view, int position, long id) -> {
if (parent.getAdapter() == null) {
subject.onComplete();
return;
}
SchoolSearchResult schoolSearchResult = (SchoolSearchResult) parent.getAdapter().getItem(position);
SharedPreferences preferences = activityReference.get().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(activityReference.get().getString(R.string.school_id), schoolSearchResult.getSchoolId());
editor.putString(activityReference.get().getString(R.string.school_name), schoolSearchResult.getSchoolName());
editor.apply();
subject.onComplete();
};
activityReference.get().binding.schoolAutocompleteSearch.addTextChangedListener(watcher);
activityReference.get().binding.schoolAutocompleteSearch.setOnItemClickListener(listener);
return subject;
}
// goes into different viewmodel
private Observable<SchoolResponse> loadSchools(CharSequence query) {
// load observable from retrofit call
}
okhttp3.internal.http2.Http2Stream.waitForIoでヌル でjava.io.InterruptedIOException(Http2Stream.java:579)okhttp3.internal.http2.Http2Stream.takeResponseHeadersで (Http2Stream.java:143) okhttp3.internal.http.RealInterceptorChain.proceedでokhttp3.internal.http.CallServerInterceptor.interceptでokhttp3.internal.http2.Http2Codec.readResponseHeadersで (Http2Codec.java:120) (CallServerInterceptor.java:67) (RealInterceptorChain。 java:92) okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java) :67) okhttp3.internal.http.RealInterceptorChain.proceedでokhttp3.internal.cache.CacheInterceptor.intercept okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92で(CacheInterceptor.java:93) ) で(RealInterceptorChain.java:67) okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)okhttp3.internal.httpで でokhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) でtorChain.java:92) 。 okhttp3.RealCall.execute(RealCall.java:63)retrofit2.OkHttpCall.executeで (OkHttpCall.javaでokhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179) でRealInterceptorChain.proceed(RealInterceptorChain.java:67) 。 174) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41) at io.reactivex.Observable.subscribe(Observab le.java:10910)io.reactivex.internal.operatorsでio.reactivex.Observable.subscribe(Observable.java:10910でretrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34) で) 。 io.reactivex.internal.operators.observable.ObservableDistinctUntilChanged $ DistinctUntilChangedObserver.onNext(ObservableDistinctUntilChanged.java:85)io.reactivex.internal.operators.observableで でobservable.ObservableSwitchMap $ SwitchMapObserver.onNext(ObservableSwitchMap.java:126) 。 ObservableFilter $ FilterObserver.onNext(ObservableFilter.java:52) at io.reactivex.observers.SerializedObserver.onNext(SerializedObserver.java:113) at io.reactivex.internal。 io.reactivex.internal.operators.observable.ObservableDebounceTimed $ DebounceEmitter.run(ObservableDebounceTimed.java:165)io.reactivex.internal.schedulersで でoperators.observable.ObservableDebounceTimed $ DebounceTimedObserver.emit(ObservableDebounceTimed.java:140) 。 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.run(ScheduledThreadPoolExecutor.java:272) at java.util.concurrent。 java.lang.Thread.runでjava.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607) でThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) (Thread.java:761)
今回は別のエラーが発生しました。コードは現在提供されています – waynejohn1110