Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android HTTP 401-在MVVM模式中使用RxJava2和Reformation2清空recyclerview_Android_Android Recyclerview_Retrofit2_Rx Java2_Android Mvvm - Fatal编程技术网

Android HTTP 401-在MVVM模式中使用RxJava2和Reformation2清空recyclerview

Android HTTP 401-在MVVM模式中使用RxJava2和Reformation2清空recyclerview,android,android-recyclerview,retrofit2,rx-java2,android-mvvm,Android,Android Recyclerview,Retrofit2,Rx Java2,Android Mvvm,我对MVVM模式和使用RxJava2和2调用API非常陌生。我正在尝试从Unsplash API调用API 我不知道我该怎么做,我看过很多教程,但没有解决问题的办法 这里是我提供APIService和OkHttpClient和改造的地方: @Provides @Singleton fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor { val interceptor = HttpLoggingInterceptor()

我对MVVM模式和使用RxJava22调用API非常陌生。我正在尝试从Unsplash API调用API 我不知道我该怎么做,我看过很多教程,但没有解决问题的办法

这里是我提供APIServiceOkHttpClient改造的地方:

 @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
}
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)
照片服务:

 @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
}
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)
视图模型:

 @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
}
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\u id
,如下所示

https://api.unsplash.com/photos/?client_id=YOUR_ACCESS_KEY
用户身份验证api可在
https://unsplash.com/oauth/


查看更多信息。

您没有访问api的权限。您需要发送一些访问令牌。检查其文档。