Android Networking: Fundamentals | raywenderlich.com

Learn about HTTP, JSON, REST and all the other cool and important abbreviations in the world of networking! Implement the Retrofit library in Android, add interceptors, parsers, and Kotlin Coroutines.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/10376651-android-networking-fundamentals

Consider adding these to Result.kt for even more simplified code:

// full combo
fun Result.listen(onSuccess:(data:T)->Unit, onFailure: (err:Throwable?)->Unit){
if(this is Success) onSuccess(this.data)
else if(this is Failure) onFailure(this.error)
}

// or each individually

fun <T: Any> Result.onSuccess(onSuccess: (data: T) β†’ Unit):Result{
if(this is Success) onSuccess(data)
return this
}

fun <T:Any> Result.onFailure(onFailure: (err: Throwable?) β†’ Unit):Result{
if(this is Failure) onFailure(error)
return this
}

and implemented:

remoteApi.addTask(AddTaskRequest(title, content, priority))
.onSuccess { onTaskAdded(it) }
.onFailure { onTaskAddFailed() }

1 Like

Hey @ooono!

Thanks for the comments and suggestions!

I typically prefer the base use where we deal just with values, but this is more Rx-style or future-style works, and it suits other people better!

But the community can choose what they like and thanks to your comment they get another option!

Thanks!

True, it’s a bit functional. Is there a course that works with network requests and kotlin flow?

There are a few pieces of content we have about Kotlin Flow, but not really about Flow for networking.

IMO, we shouldn’t be using Flow for networking requests, as those are mostly one-off and basic coroutines are good enough. I’d rather connect the Flow to the DB (Room e.g.) and then listen to changes there.

Here is the content I mentioned:
https://www.raywenderlich.com/10892694-room-database-getting-started
https://www.raywenderlich.com/9147615-kotlin-flow-getting-started
https://www.raywenderlich.com/9799571-kotlin-flow-for-android-getting-started

Thanks!