https://kotlinlang.org logo
#android
Title
# android
b

Bruno_

02/21/2020, 8:56 PM
I'm doing an interview homework that's an android app, the only thing that's left to do is network error handling I have no idea how to approach this, could you help?
i've tried implementing BroadcastReceiver but it wouldn't work
this piece of code is working but I'd have to setup a loop that would check the connection, I don't think that's the best idea
Copy code
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
val isConnected: Boolean = activeNetwork?.isConnectedOrConnecting ?: false
b

Brian

02/21/2020, 9:01 PM
you can monitor for changes in connectivity using
ConnectivityManager.NetworkCallback
s

satyan

02/21/2020, 9:02 PM
Read ConnectivityManager documentation you should find something useful to handle the offline case. To handle network errors upon requests you should read your HTTP client documentation (Retrofit on top of OKHttp ? Java HTTP ? ) This Slack is supposed to be about Kotlin so it might not be the best place to ask interview homework questions. Try doing the Sunshine app application course on Udacity there must be a part about network calls.
b

Bruno_

02/21/2020, 9:05 PM
thanks, that'll solve the problem
or it won't, I have to support API14, would it be a good idea to listen for a flowable error somewhere?
not sure if it's a good idea tho, I've separated my retrofit module and I return flowable from it, the only place that has the flowable is a repository
this one works
r

Ryan Mentley

02/21/2020, 11:19 PM
as it happens I was semi-recently helping update one of the training articles on exactly this topic: https://developer.android.com/training/monitoring-device-state/connectivity-status-type
note specifically the bit about ConnectivityManagerCompat
a

Anastasia Finogenova

02/22/2020, 5:04 AM
You can always go for a retey button, on a snackbar or a dialog or even come up with a new UI for the network error state with the retry option , it is very straightforward imo
The network error will be returned differently depending on the network library you use. You mentioned you use Flowable so apparently RxJava, your network error is gonna fall in the the onError callback. So inside the onError callback check if the error is instance of Sockettimeoutexception or unknownhostexception if using retrofit and then just show your network error prompt with the retry
b

Bruno_

02/22/2020, 12:13 PM
the only place that has access to the flowable is the repository, which means that I can do
doOnError
only there. would it be a bad idea to throw an error in a repository and catch it in a fragment using a global exception handler? https://stackoverflow.com/questions/4427515/using-global-exception-handling-on-android
other solution would be to somehow use rxjava flowable instead of a view model (so fragment would call repository directly instead of fragment -> view model -> repository flow), is that a good idea?
I should probably mention that I use MVVM
@Anastasia Finogenova @Ryan Mentley
it's more of an architecture question, I know how to handle the rror, display a snackbar etc, I just don't know how to design the module properly
r

Raul Tunduc

02/22/2020, 12:25 PM
❤️ 1
a

Anastasia Finogenova

02/22/2020, 8:57 PM
Just return this Flowable from the repo method and subscribe to it in the view model and do actions on result on success or on error
👍 1
Throwing an error in the repo and catching in the fragment is a very bad not straightforward idea
r

Raul Tunduc

02/23/2020, 8:28 AM
From my point of view, I would NOT recommend using RX for HTTP requests. It's easier with Coroutines
a

Anastasia Finogenova

02/23/2020, 7:33 PM
It is not easier imo, after using coroutines a lot I realized they are not easier in any way that RxJava from the dev stand point
I am recommending Flowable as he said he already has it
m

Mikael Alfredsson

02/24/2020, 9:28 AM
Since its an interview assignment, shouldn’t be able to solve it if youre qualified for the position? 🧌 The error you should get back when you are trying to call the endpoint, so a normal try/catch works for that, and for the download automatically/retry, I would go for the first option and use Workmanager to achieve that.
b

Bruno_

02/24/2020, 2:31 PM
@Mikael Alfredsson it was my first real android app 😄 I'm a fullstack dev on a daily basis anyway, what I did was pretty much what you can see in the last SOF link I've sent - throwable field in a view model that I check on every view model update
5 Views