Reputation: 509
I have made a REST API for an app I have. I have secured it with JWT bearer tokens. I have made a website and an android app for this application
The website is made with ReactJS. I used this pattern to handle users and requests:
related items are put in one place.
Now to the android app ..
okhttp
in a method inside the activity class that uses it. This is quite buggy because sometimes I need to use this same method outside of that activity. I tried making a Request class but then I couldn't use it and making it static will not make me be able to use runOnUiThreadsharedpreferences
. BUT I don't thing accessing shared preferences each time I need user is optimal. I made a static field user in main activity. and all other activities which needs it can access it with no problem. still buggyisn't there a better way to manage all of this? my code at the moment runs but it's pretty much spagetti. Plus there are some functions which I would like to implements before each request like checking the token and refreshing it if needed or logging out the user if token is expired. I looked around but didn't see any thread talking about android REST API architecture
tl;dr: my android architecture for handling the user and requests now works. but it is very buggy. How can I improve this? are there already made architectures that I can implement?
Upvotes: 0
Views: 207
Reputation: 76476
You can do a redux architecture on Android to get your app a little bit more similar to the ReactJs site https://jayrambhia.com/blog/android-redux-intro
For middle ware you will probably want to use Interceptors, tho I realise that doesn't help with architecture https://square.github.io/okhttp/interceptors
For higher order components to check user eligiblity, "use cases" come to mind in clean architecture, you can read this https://proandroiddev.com/kotlin-clean-architecture-1ad42fcd97fa or just good "clean architecture android"
Upvotes: 3