-1

我对MVVM 模式和使用RxJava2Retrofit2调用 API非常陌生。我正在尝试从Unsplash API调用 API 我不知道该怎么做,我查看了很多教程,但没有解决正在发生的事情。

这里是我提供APIServiceOkHttpClientRetrofit 的地方:

 @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
}

视图模型:

  //=======================================================
//                  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
}

和我设置适配器的片段:

 private fun setPhotos() {
    viewModel.getPhotos().observe(this, Observer<List<Photo>> {
        it -> rvListPhoto.adapter = PhotosAdapter(it)
    })
}

我认为来自适配器的代码不一定要放。因为错误来自另一边,但我不知道问题出在哪里。

如果它确实很重要,那就是错误:

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)
4

1 回答 1

1

查看 unsplash 的文档,您似乎需要将client_id查询作为查询参数传递,如下所示

https://api.unsplash.com/photos/?client_id=YOUR_ACCESS_KEY

client_id可以通过用户身份验证 api 在https://unsplash.com/oauth/

检查https://unsplash.com/documentation#user-authentication了解更多信息。

于 2018-11-22T11:39:21.387 回答