HTTP 401 - 在 MVVM 模式中使用 RxJava2 和 Retrofit2 的空 recyclerview
HTTP 401 - empty recyclerview with RxJava2 and Retrofit2 in MVVM Patterns
我对 MVVM Patterns 很陌生,并且使用 RxJava2 和 Retrofit2[= 调用 API 45=]。我正在尝试从 Unsplash API 调用 API
我不知道该怎么办,我查看了很多教程,但没有解决正在发生的事情。
这里是我提供APIService和OkHttpClient和Retrofit:
@Provides
@Singleton
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return interceptor
}
@Provides
@Singleton
fun provideOkHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
val okHttpClient = OkHttpClient.Builder()
okHttpClient.apply {
if (BuildConfig.DEBUG) {
addNetworkInterceptor(StethoInterceptor())
addInterceptor(httpLoggingInterceptor)
}
}
return okHttpClient.build()
}
@Provides
@Singleton
fun provideApiService(okHttpClient: OkHttpClient): PhotoService {
val retrofit = Retrofit.Builder()
.baseUrl("https://api.unsplash.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
return retrofit.create(PhotoService::class.java)
}
照片服务:
@GET("photos")
fun getPhotos(@Query("page") page: Int = 1): Observable<List<Photo>>
照片库:
fun getPhotos() : PhotoService {
return photoApiService
}
ViewModel:
//=======================================================
// DISPOSABLE
//=======================================================
private var disposable: Disposable? = null
//=======================================================
// GET PHOTOS
//=======================================================
private val photoLive = MutableLiveData<List<Photo>>()
private val photoData: LiveData<List<Photo>>
get() = photoLive
@SuppressLint("LogNotTimber", "CheckResult")
fun getPhotos(): LiveData<List<Photo>> {
disposable = photoRepository.getPhotos().getPhotos()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
it -> photoLive.value = it
},
{
it -> if (it != null)
{
Log.e(TAG,Log.getStackTraceString(it))
}
})
return photoData
}
和 Fragment 我设置适配器的地方:
private fun setPhotos() {
viewModel.getPhotos().observe(this, Observer<List<Photo>> {
it -> rvListPhoto.adapter = PhotosAdapter(it)
})
}
我认为来自Adapter的代码不一定要放。因为错误来自另一边,但我不知道问题在哪里。
如果它确实重要这里是错误:
retrofit2.adapter.rxjava2.HttpException: HTTP 401
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:54)
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:44)
at io.reactivex.Observable.subscribe(Observable.java:12005)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:12005)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:571)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
查看 unsplash 的文档,您似乎需要将 client_id
作为查询参数作为查询参数传递,如下所示
https://api.unsplash.com/photos/?client_id=YOUR_ACCESS_KEY
这个client_id
可以在https://unsplash.com/oauth/
通过用户认证api找到
查看 https://unsplash.com/documentation#user-authentication 了解更多信息。
我对 MVVM Patterns 很陌生,并且使用 RxJava2 和 Retrofit2[= 调用 API 45=]。我正在尝试从 Unsplash API 调用 API 我不知道该怎么办,我查看了很多教程,但没有解决正在发生的事情。
这里是我提供APIService和OkHttpClient和Retrofit:
@Provides
@Singleton
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return interceptor
}
@Provides
@Singleton
fun provideOkHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
val okHttpClient = OkHttpClient.Builder()
okHttpClient.apply {
if (BuildConfig.DEBUG) {
addNetworkInterceptor(StethoInterceptor())
addInterceptor(httpLoggingInterceptor)
}
}
return okHttpClient.build()
}
@Provides
@Singleton
fun provideApiService(okHttpClient: OkHttpClient): PhotoService {
val retrofit = Retrofit.Builder()
.baseUrl("https://api.unsplash.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
return retrofit.create(PhotoService::class.java)
}
照片服务:
@GET("photos")
fun getPhotos(@Query("page") page: Int = 1): Observable<List<Photo>>
照片库:
fun getPhotos() : PhotoService {
return photoApiService
}
ViewModel:
//=======================================================
// DISPOSABLE
//=======================================================
private var disposable: Disposable? = null
//=======================================================
// GET PHOTOS
//=======================================================
private val photoLive = MutableLiveData<List<Photo>>()
private val photoData: LiveData<List<Photo>>
get() = photoLive
@SuppressLint("LogNotTimber", "CheckResult")
fun getPhotos(): LiveData<List<Photo>> {
disposable = photoRepository.getPhotos().getPhotos()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
it -> photoLive.value = it
},
{
it -> if (it != null)
{
Log.e(TAG,Log.getStackTraceString(it))
}
})
return photoData
}
和 Fragment 我设置适配器的地方:
private fun setPhotos() {
viewModel.getPhotos().observe(this, Observer<List<Photo>> {
it -> rvListPhoto.adapter = PhotosAdapter(it)
})
}
我认为来自Adapter的代码不一定要放。因为错误来自另一边,但我不知道问题在哪里。
如果它确实重要这里是错误:
retrofit2.adapter.rxjava2.HttpException: HTTP 401
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:54)
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:44)
at io.reactivex.Observable.subscribe(Observable.java:12005)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:12005)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:571)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
查看 unsplash 的文档,您似乎需要将 client_id
作为查询参数作为查询参数传递,如下所示
https://api.unsplash.com/photos/?client_id=YOUR_ACCESS_KEY
这个client_id
可以在https://unsplash.com/oauth/
查看 https://unsplash.com/documentation#user-authentication 了解更多信息。