HTTP 401 - empty recyclerview with RxJava2 and Retrofit2 in MVVM Patterns












-1















I am pretty new to MVVM Patterns and calling API with RxJava2 and Retrofit2. I'm trying to call API from Unsplash API
I have no idea what should I do, I've looked over a lot of tutorials but no solution what is happening.



Here is where I provide APIService and OkHttpClient and 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)
}


PhotoService:



   @GET("photos")
fun getPhotos(@Query("page") page: Int = 1): Observable<List<Photo>>


PhotoRepository:



  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
}


And Fragment where I set adapter:



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


I think code from Adapter is not necessarily to put it. Because the error is from the other side but I don't know where is the problem.



And if it does matters here is the error:



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)









share|improve this question


















  • 1





    You don't have permission to access the api. You need to send some access token. Check its documentation.

    – Rohit5k2
    Nov 22 '18 at 11:36


















-1















I am pretty new to MVVM Patterns and calling API with RxJava2 and Retrofit2. I'm trying to call API from Unsplash API
I have no idea what should I do, I've looked over a lot of tutorials but no solution what is happening.



Here is where I provide APIService and OkHttpClient and 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)
}


PhotoService:



   @GET("photos")
fun getPhotos(@Query("page") page: Int = 1): Observable<List<Photo>>


PhotoRepository:



  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
}


And Fragment where I set adapter:



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


I think code from Adapter is not necessarily to put it. Because the error is from the other side but I don't know where is the problem.



And if it does matters here is the error:



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)









share|improve this question


















  • 1





    You don't have permission to access the api. You need to send some access token. Check its documentation.

    – Rohit5k2
    Nov 22 '18 at 11:36
















-1












-1








-1








I am pretty new to MVVM Patterns and calling API with RxJava2 and Retrofit2. I'm trying to call API from Unsplash API
I have no idea what should I do, I've looked over a lot of tutorials but no solution what is happening.



Here is where I provide APIService and OkHttpClient and 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)
}


PhotoService:



   @GET("photos")
fun getPhotos(@Query("page") page: Int = 1): Observable<List<Photo>>


PhotoRepository:



  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
}


And Fragment where I set adapter:



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


I think code from Adapter is not necessarily to put it. Because the error is from the other side but I don't know where is the problem.



And if it does matters here is the error:



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)









share|improve this question














I am pretty new to MVVM Patterns and calling API with RxJava2 and Retrofit2. I'm trying to call API from Unsplash API
I have no idea what should I do, I've looked over a lot of tutorials but no solution what is happening.



Here is where I provide APIService and OkHttpClient and 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)
}


PhotoService:



   @GET("photos")
fun getPhotos(@Query("page") page: Int = 1): Observable<List<Photo>>


PhotoRepository:



  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
}


And Fragment where I set adapter:



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


I think code from Adapter is not necessarily to put it. Because the error is from the other side but I don't know where is the problem.



And if it does matters here is the error:



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)






android android-recyclerview retrofit2 rx-java2 android-mvvm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 11:20









androidandroid

265




265








  • 1





    You don't have permission to access the api. You need to send some access token. Check its documentation.

    – Rohit5k2
    Nov 22 '18 at 11:36
















  • 1





    You don't have permission to access the api. You need to send some access token. Check its documentation.

    – Rohit5k2
    Nov 22 '18 at 11:36










1




1





You don't have permission to access the api. You need to send some access token. Check its documentation.

– Rohit5k2
Nov 22 '18 at 11:36







You don't have permission to access the api. You need to send some access token. Check its documentation.

– Rohit5k2
Nov 22 '18 at 11:36














1 Answer
1






active

oldest

votes


















1














Looking at the documentation of unsplash, seems like you need to pass client_id with your query as a query parameter like below



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


This client_id can be found by user authentication api at https://unsplash.com/oauth/



Check https://unsplash.com/documentation#user-authentication for more information.






share|improve this answer



















  • 1





    Thank you, that was the problem...

    – android
    Nov 22 '18 at 13:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429834%2fhttp-401-empty-recyclerview-with-rxjava2-and-retrofit2-in-mvvm-patterns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Looking at the documentation of unsplash, seems like you need to pass client_id with your query as a query parameter like below



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


This client_id can be found by user authentication api at https://unsplash.com/oauth/



Check https://unsplash.com/documentation#user-authentication for more information.






share|improve this answer



















  • 1





    Thank you, that was the problem...

    – android
    Nov 22 '18 at 13:25
















1














Looking at the documentation of unsplash, seems like you need to pass client_id with your query as a query parameter like below



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


This client_id can be found by user authentication api at https://unsplash.com/oauth/



Check https://unsplash.com/documentation#user-authentication for more information.






share|improve this answer



















  • 1





    Thank you, that was the problem...

    – android
    Nov 22 '18 at 13:25














1












1








1







Looking at the documentation of unsplash, seems like you need to pass client_id with your query as a query parameter like below



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


This client_id can be found by user authentication api at https://unsplash.com/oauth/



Check https://unsplash.com/documentation#user-authentication for more information.






share|improve this answer













Looking at the documentation of unsplash, seems like you need to pass client_id with your query as a query parameter like below



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


This client_id can be found by user authentication api at https://unsplash.com/oauth/



Check https://unsplash.com/documentation#user-authentication for more information.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 11:39









Rohit5k2Rohit5k2

14.2k42551




14.2k42551








  • 1





    Thank you, that was the problem...

    – android
    Nov 22 '18 at 13:25














  • 1





    Thank you, that was the problem...

    – android
    Nov 22 '18 at 13:25








1




1





Thank you, that was the problem...

– android
Nov 22 '18 at 13:25





Thank you, that was the problem...

– android
Nov 22 '18 at 13:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429834%2fhttp-401-empty-recyclerview-with-rxjava2-and-retrofit2-in-mvvm-patterns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga