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
Related
Apologies if this is too simple of a question, I am new to using remote data sources.
I assume Enqueue is running on background threads instead of main thread, but which is faster and better for optimization? As I understand using Runnables will take up more code, but I have seen multiple apps built with such a method, is it better than the simpler Enqueue method?
Retrofit will use underlying OkHttp to make calls to the server. Enqueue is tested and it is always better to use the globally recognized by developers with performance testing and lots of other aspects around it. It also covers your ExecutorService without you having to write the implementation for it. I'll add few points for readers new to OkHttp.
new Request.Builder().url(endpoint).build() creates the request but
doesn't send anything.
client.newCall(request).execute() sends the request and waits for
the response, but doesn't download the response, only its headers so
you can check things like response.isSuccessful() immediately.
response.body().string() downloads the body of the response and
returns a string.
You can push your own implementation of ExecutorService like this
OkHttpClient.Builder().dispatcher(Dispatcher(executorService)).build()
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.
I'm using Volley for all my network related code. Since Volley does the actual networking part off the main UI thread I havent really thought much about it but I realize now that in a few instances I do some significant processing of the response data in onResponse(). It seems like this code does indeed run on the main thread. What's the best way to do this? Should I define a AsyncTask for that specific portion of the code or should I just put the entire network request in a AsyncTask despite Volley's threading mechanisms.
I would suggest NOT to use AsyncTask as it has its own drawbacks in case activity is destroyed or recreated. And moreover if you are using Volley's queueing mechanism, this will just work as a add-on.
One solution I can think of is to use Loaders. Make the network call and process the response in loadInBackground method. And when onLoadFinished is called, you'll get the processed response.
Another solution (Not recommended and not so efficient) is to use a Service. You can make the network call from the Service and process the data and communicate back to the Activity.
I have asked question recently on stackoverflow regarding my slow network calls using Retrofit and RxJava
I have made some changes of scheduler.io in it and removed main thread from all the execution. But still the result is not that much satisfying. I have digged in the code of OkHttp and Retrofit. Below are my findings:
Retrofit uses scheduler.io which will execute all the requests in the threadpool. Previously it was creating new threads. Does changing it to scheduler.io from newthread make any difference?
Now OkHttp has limit of max requests per host of 5 in the code. So does that make any difference?
All the calls are happening in Dispatcher using enqueue. Do we need to set some configuration to Retrofit and OkHttp for that?
Any suggestions to increase the performance of webservice call will be appreciated?
I am using the Retrofit 2 library for an android REST client. Retrofit itself supports synchronous and asynchronous request (cf. here), the reason for the latter being not to block a thread and thus not to get interrupted by android.
In practice, is it better to use synchronous calls in a native AsyncTask or asynchronous calls directly from Retrofit?
If one is preferable over the other, what are the technical reasons?
One of the main reasons to use any of the popular REST clients (retrofit, volley, etc) is that they reduce the amount of details you have manage at the application layer. One of those details is making sure your network requests happen off the main thread. Why would one use an AsyncTask when a library they are already using for other features provides the same functionality with less ceremony? The only reason I can think of is -- you don't think the library's threading is very good. That concern does not apply to retrofit 2, it uses OkHttp to dispatch async calls. OkHttp has been around awhile and used extensively, it manages its own thread pool to execute async requests, and is solid.
So, the upside to using retrofit async is cleaner code, and no downside I know of vs AsyncTask with retrofit sync calls. The only time I use the sync calls is when my code is already executing in a background thread for another reason. I never create a separate thread or asynctask just for the network call and use enqueue instead.