I am new to Android development and it's my first time on API. I am getting a message log that application main thread may working too much . I am using Retrofit with enqueue callback, a java class Model.
I just want to take advice from you guys that should I use AsyncTask to make application working smooth? or Retrofit is already doing the work like that of AsyncTask? or is there a need or not of AsyncTask in my application?.
I have been searching this for few days but I am confused
Note
I am using the Picasso library for downloading the image and an
image would be 100 kb in size.
Also as the application is still in its build there is no too much data on the server.
Purpose
I just want the application to work smooth and good on all devices.
AsyncTask is deprecated
Here.
Retrofit has its own async calls mechanism which is far more appropriate
Here.
Also, you can use rxJava with retrofit which will help you even more in creating async calls.
Related
I recently switched to Retrofit, and very new to the retrofit concepts. I found out about it Here and found it very interesting and made my work more easier and simpler.
As per the rules any HTTP request must be in a AsyncTask extended class,
from the above tutorial I found that the retrofit call are made in the main UI thread itself.
I am finding it difficult to implementing the same tutorial in a AsyncTask class. How can I do this?
please help me as for every retrofit call my app is betting slower and I get skip frames 1076.
There is 2 methods in retrofit2, First is execute that will call the request synchronously. The second is enqueue method that will make the request asynchronously.
As per the rules any HTTP request must be in a AsyncTask extended class
You misunderstand - A network request cannot be run on the UI thread. An AsyncTask just happens to be one way to do that. Retrofit/OkHttp is another, and Volley, and AsyncHttpClient are others.
I am finding it difficult to implementing the same tutorial in a AsyncTask class
Probably that is because that tutorial is specifically for Retrofit?
How can I do this?
A high-level overview of what Retrofit gives you that you would have to toss together in an AsyncTask
Building an HTTP Request
Adding HTTP Headers (if needed)
Reading an InputSteam to a String
String to JSON conversion
JSON to Java Object Conversion
Callbacks to the main thread
Now, that all seems complex if you don't really understand that stuff. That being said, you should just stick to Retrofit's enqueue
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.
I am developing an android application that needs to communicate a lot with a remote server. For this task I wrote a class that handles all the network communication.
Do I need to make for every single method as an Asynctask? What about methods that I am dependent on for the rest of the code execution? (thus needs to be done synchronously - like waiting for an answer on registration?)
I am asking this because I already had a small app before to communicate with a remote server and I didn't use any Asynctasks, this one crashes on every method being called though.
Edit -
Meanwhile - before making a class of my own I found a great tutorial related to a google libraray that already handle that the libraray name is Volley the tutorial I looked on is this one
Yes, every network call has to asynchronous. You don't need to make every single method in you class async, but i would refactor the code in a way that only one peace of code actually does the calls and this peace has to be async. If you have following code that depends on the response from the server, then use a callback.
In pseudo code that would look something like this:
void makeNetworkCall(command, listener){
async(){
result = command.execute();
listener.onCommandSuccess(result);
}
}
Do I need to make for every single method as an Asynctask?
Yes. Android requires networking code to be executed asynchronously, so the user interface never gets blocked.
What about methods that I am dependent on for the rest of the code execution?
You can wait for an Asynchtask to finish execution. Refer to this question.
They tell everywhere that we should use ASyncTaskLoaders because they are very good at not blocking the UI thread. And there is Volley to use now.
I guess we cannot use both of them because Volley does backgrounding on its own. What do you think? Which one is better?
These 2 technologies are different and hardly comparable. They have different purposes and can also work together. You could for exemple implement a Loader that uses Volley to load the data and inherits directly from Loader (not AsyncTaskLoader, because Volley handles the threading as well).
Main advantages of using Loaders:
Lifecycle is synchronized with the Activity/Fragment lifecycle automatically
Data and loading state is not lost on configuration change
The loader monitors changes and pushes new results automatically to the client fragment/activity.
Main advantages of using Volley:
High performance network stack
Automatic disk cache that respects the HTTP server policy
Powerful cancelation mechanism.
You can combine both to get both sets of advantages or you can use Volley without loaders with its simple API.
I've been using Volley for a month now and I have to say that I'm very satisfied. It really does help a lot not to have to worry about threading implementation details. So far both general networking and remote image loading have been working great.
It's not that there are no issues, but so far they have been minimal.
You better ask like this volley vs Async vs RxJava
You can use this RXJava for background thread, but for better efficiency in calling restful services Volley is highly recommended, also very less coding required compare to async task loaders !
Here is a writeup on current Android best practices. It discusses the use of Volley and RXJava: https://github.com/futurice/android-best-practices
You can combine both, to get both advantages.
In your activity (main thread) you call your API with Volley.
With a simple interface mechanism, you callback your main thread when the data is available.
Then you forceLoad() your AsyncTaskLoader with the fresh data from Volley.
In your AsyncTaskLoader you hydrate all your activity's container. They will be automatically loaded when data is available.
With this approch you combine Automatic disk cache of Volley, and Automatic synchronization of Loader.
I'm writing an android app that will connect to a REST/JSON webservice. Users will be retrieving information, uploading comments, downloading and uploading images etc.
I know that I shouldn't keep all this network communication in the Activity/UI thread, as it will cause ANRs. What I'm confused about is whether I should use an AsyncTask or a Service with "manual" threading to accomplish this;
With a Service, I'd simply have a public method for each method in the webservice's API. I'd then implement threading within each of these methods.
If I used an AsyncTask, I would create a helper class that defined AsyncTasks for each method in the webservice's API.
Which method is preferred? Interaction with the webservice will only happen while the user is in the Activity. Once they switch to another application, or exit the program, there is no need for communication with the webservice.
I recommend you go for the AsyncTask solution. It is an easy and straightforward way of running requests or any other background tasks of the UI-thread.
It's also easy to implement e.g. onProgressUpdate if you need to show a progress bar of some sort while running your requests.
I recommend IntentService, it is not much more complex to implement and is definitely more robust because it is not tied so closely on the ActivityLifecycle (in particular to onConfigurationChange())
This library provides an async wrapper to Apache httpclient available in Android.
http://loopj.com/android-async-http/