I'm looking for the best way to implement try-catch when Android client meet StatusRuntimeException.
I know I can write try-catch block to every single gRPC api call and it will work, but I don't want to do.
Many boiler try-catch blocks would make my source code hard to maintain.
I've found UncaughtExceptionHandler, but it doesn't look like what I wanted.
I'd like to show AlertDialog on the screen, which called gRPC API and met StatusRuntimeException. As far as I know, UncaughtExceptionHandler needs to start a new Activity.
What should I do to avoid a pile of try-catch blocks?
Related
I'm working on an Andriod app. I was examining my work from a high-level perspective, and there seemed to be a very high branching factor of potential [auth-based] crashes that could occur stemming from the fact that authorization is asynchronous using Firebase.
My attempt at solving this (simplistically put) was to put pretty much all my code into the callback of the Firebase auth result (but then I had issues with race conditions related to the Google API Client [I use location] connection callback, along with other small issues).
After doing what I could to make it as watertight as possible, I'm a little dissatisfied with the way my code is laid out in my project. E.g. In my MainActivity's onCreate method, I wish I could just lay everything out linearly to be executed one after the other (but I am aware of the UX drawbacks of not having things being done asynchronously).
Given this, I'm wondering if anyone knows of any better ways to do this or best practices when it comes to this sort of thing. Is the best solution just to put all the code into the callbacks of methods on which it depends? Or is there something I'm missing?
Thanks
yes there is , your structure is asynchronous in nature.To avoid race conditions every condition must trigger accordingly. The glorious concept of promises perfectly does this job for you.Promises return an object…which promises to do some work.This object has separate callbacks…for success and failures.
This let's us work with asynchronous code…in a much more synchronous way.…A really nice feature is that promises…can be combined into dependency chains. Which simply means execute this, if that happens or fail that. Please consider using rxjava or rather rxandroid which embodies the notion of observable.Observables can be used to model events, asynchronous requests, and animations.
Observables can also be transformed, combined, and consumed.
is the best solution just to put all the code into the callbacks of methods on which it depends? well like you said it depends , what you should is to separate tasks into single dependencies not one big monolithic callback.
I am a Java developer with no Android experience, and I am trying to quickly put an app together. It seems that what I would normally do in Java isn't helping.
At this stage, ease of implementation is more important than efficiency or style - I will sort the latter out when there is more time and I will have educated myself properly when it comes to Android.
People can use the app to ask for support, or offer it to those who need it. Asking for support posts a request with the details to the server, and that's done.
Now I would like the app to post an asynchronous request to the server, to be notified of outstanding support requests once a minute. I guess it's the same principle of WhatsApp checking if there is any new message on the server.
I tried doing that in a separate thread with an infinite loop which sleeps for 60 seconds but for some reasons that stops the UI from working.
From what I now understand, I should use a service with a Looper, a Timer and a Handler. Is that correct?
Could anybody point me to a tutorial which explains exactly what to do, step by step? Or at least suggest keywords I should look for?
All I found so far are snippets of code which don't work together when I try to assemble it. Possibly because I am not searching for the right terms?
Thanks, Dan
You could try the following approach:
Create a service that runs in the background to check for newly added data in the server.
If you prefer to make it user-driven, you can let users refresh the list on the device to actually trigger the requests to the server.
Libraries like Retrofit can make your life easier when it comes to making http requests - always avoid the main UI thread when doing this.
Another library that you could use to decouple your application using Events is EventBus. Assuming you are running a background service to check for updates, you can use EventBus to update your User Interfaces when something new is retrieved from the server through a GET request.
I hope this gives you an idea on how to proceed with the solution. Good luck!
I'm new to Android Development, and I've run into this problem that I haven't found a solution for.
It starts off first with going to a webservice api for login. From there if the login is successful it executes to 2 functions for the actually data it needs, stores in sqlite and then proceeds to next activity. All 3 api requests are using AsyncTask and from what I understand my Activity is actually running faster than my "doInBackground" background thread. I want to know the path or what i should look into. I've read posts about using sleep, and read posts about how that is bad to do. I want to get the json data i need, store it, and use it immediately. I think i'm suppose to find away to connect directly and use a progress bar to get the data. Keep in mind, it's not a lot of data, but it's enough to stall my application.
Not sure what a ProgressBar has to do with retrieving data from a server but if you're looking for AsyncTask alternatives (particularly for HTTP calls) you can look at these frameworks (you'll probably only want to pick one):
Square's Retrofit
Google's Volley
Either one will make your life a lot easier when it comes to making HTTP requests. Their own documentation explains how to use them pretty well so I'm not going to go into how to use it here.
If you're looking for a native, lower level AsyncTask alternative, have a look at AsyncTaskLoaders. The AsyncTaskLoader essentially does exactly the same thing as an AsyncTask but they live within the life cycle of the Activity or Fragment so your code tends to be less error prone.
My question is kinda theorical, hope I can get a clear explanation on this.
I've been looking for a nice rest api consumer for android (or some clear info on how to develop a solid one) and I found the rest api design talk from google IO 2010.
"Developing Android REST Client Applications - Google"
It's been 4 since this talk and I think there might exitst new designs and techniques for this matter, or not ?
The scenario that I think that would work the best for me is this one:
So my first question is, does this architecture is still valid for a new app (Starting from the beginning) ?
I've found Retrofit, which seems a pretty nice and stable Api for the rest service, but I can't quite understand how it works, like if it is a good approach to call my api endpoints from activities (or frags) and the library handles the resume/pause (delivering results when activity is on hold, or not) or I must implement this myself.
Sorry for the long post and thanks for the patience !
You must implement how to handle that yourself, to answer your question.
Yes, that architecture is valid, but RetroFit only forms part of the "Rest Method" block. How would you implement it? That depend on what you want. You could use Retrofit (AKA the REST Method) inside the service, and that would be ok, however you can also skip using services and use it inside of an activity or fragment. However if you do the second option(activities/fragments) you can not think in handling the activity life cycle within retrofit(onStart, onPause, etc..) because since these are network calls, you must perform them on worker threads(AsyncTasks for instance).
So in conclusion, whatever option you decide, being Services or worker threads, you must think in using perhaps observers to control your activity/fragment, and remember that requesting network data is always done out of the ui thread. Implementations of how to handle the situation may be done by you depending on your needs and complexity of the application.
I'm very new to android technologies. I've recently read that android only allows a REST web service invocation from inside an AsyncTask... Is this true?? I'm developing an app for the university, I have to finish it for tomorrow and I would realy like to know if I can just call the REST WS inside an ordinary function, despite the fact that it may not be a good practice...
Thank you in advice!!
José.
The main thing is that you may not call it on the UI-thread (otherwise you will get an exception). Besides this restriction, it does not matter from where you call it.
A benefit of using AsyncTasks is that they are a very easy way of using additional threads in Android, since it comes with many callbacks (which are run on the UI-thread)
Another alternative could be to use an ExecutorService.