Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving/Serializing OkHttp/Retrofit Requests

I want to be able to recover from crash/closing the app or just device being disconnected.

Currently when I detect that the network is out for my Android device I save the Call created with RetroFit2 in a stack (to process later). If the user were to close the app or restart the device I lose the possibility to save these calls anywhere...

My question is the following, how can I save a RetroFit Call or an OkHttp3 Request? None of them is serializable or nor can I convert them to strings from what I could see looking at the code.

like image 768
Victor Avatar asked Oct 19 '16 04:10

Victor


People also ask

Which is better OkHttp or retrofit?

You should use retrofit if you are trying to map your server API inside your application (type-safing). Retrofit is just an API adapter wrapped over okHTTP. If you want to type safe and modularise the interaction code with your API, use retrofit.

Does Retrofit use OkHttp?

It started as a component of OkHttp. Retrofit is a type-safe REST client for Java and Android application development. It consists of interfaces, classes, and methods that provide the required functionalities. JSON or XML data may be parsed and converted to POJOs (Plain Old Java Objects) using the Retrofit library.


1 Answers

Use android priority jobqueue by Yigit Boyar (one of the google android guys). It'll serailize your jobs, detect network changes (and respond accordingly) and persist even through device reboots (let alone app crashes). Plus a ton of other features. Just take a look. It is not exactly what you requested but it's a better solution. It's Magic.

Starting with v2, Job Queue can be integrated with JobScheduler or GCMNetworkManager. This integration allows Job Queue to wake up the aplication based on the criterias of the Jobs it has. You can see the deatails on the related wiki page. The Scheduler API is flexible such that you can implement a custom version of it if your target market does not have Google Play Services.

Try it and you'll be glad you did, as I've been. It filled the huge gap in my code that I spent weeks hacking together with spit, ducktape and faith.

like image 158
Elvis Chweya Avatar answered Sep 24 '22 05:09

Elvis Chweya