There are a few Fragments in my main activity. Different adapters are used in different fragments. Those adapters are used to make http requests to my web services.The requests work well except that it requests too frequently sometimes, for example when the device rotates. Actually I don't want to request if the last request is not long enough, for example, no more than 30s. But the problem is the Fragments will be recreated when the device rotates. So that the adapter will also be recreated and then the http request will be made.
Of course I can create a global queue for the request so that I can ignore the request when last the request is no more than 30s. But the fragment will be empty if I don't request because it's a brand new fragment.
Can anybody advise what the best practice for a proper http request in Android? Thanks
How about using an IntentService or ContentProvider to manage the requests i.e maintain a timestamp of the last request and only process the web service request if the last request was made X seconds ago.
While the fragment is waiting for content, show a ProgressBar (indeterminate = true)
Related
I've been reading that we dont need to use AsyncTask when working with volley. I had a question though, one of the button click in my activity triggers a HTTP request. Now in case my app is closed, will volley still be able to process that request.
When I launch my app again, is it possible to figure if my previous HTTP request was sent or not.
What would happen to the response? Will I be able to catch the response and maybe make some db updates (local to app)?
What is the right design to do these kind of http requests?
Also, in case I want to make some HTTP requests while the app is not launched, can I not use a background task to do all this with volley?
You don't need to use an AsyncTask or Thread for Volley because Volley does that for you. So that's correct.
When you say your app is closed- do you mean the app is in the background, or the app is terminated? In the first case, the request will continue. In the second, it will not, and any response coming in would be ignored at the OS layer because there's no listening app on the socket. Also remember that apps not in the foreground can be killed by the OS at any time.
You can't send an HTTP request when your app isn't launched. You can do so in a JobScheduler or WorkManager item, but then your app is launched, you just aren't showing a UI.
In my app I use Retrofit and I have such situation:my app contains bottomNavigationView with 5 items which are assigned to a certain fragment, each fragment at onCreateView() method has method call at which we send request to API. I can create a situation when I can send several requests when I tap on different items for replacing current fragment. I would like to prevent sending several requests if it's possible. I have some thoughts about it:
Use dispatcher at retrofit client with max requests - setMaxRequests(1)
Make retrofit client to clear all request - client.dispatcher().cancelAll()
Check when current fragment is visible to user and make request - userVisibleHint
Or someone had similar problem and knows how to solve it. What I see at logs after tapping several times for example on one item of bottomNavigationView - several requests and several responses. Hope you will help me :)
Your fragments can be recreated everytime you replace fragment. This means you send request each replacement. You can prevent this situation aplying this solution.
I am currently storing my transactions in SqLite DB if there is no internet connection at the moment and when there is internet available i send the pending transactions with the new one made so making it quite a lot of data and a lot of API hits which chokes the device and it becomes unresponsive. So need help with a proper way to sync these transactions to the server. Also these are being sended to a Socket as well as a Server.
I tried using AsyncTask for it but it also causes problems if transactions are above 200. Tried Retrofit for it and to some extent the count exceeded from 200 to almost 350 but the issue and unresponsivness remains.
You should give a try to PriorityJobScheduler lib or WorkManager from JetPack.
When there is no network connection you can queue those request and those request will be send ones network connection is available. (So you dont need to wait until someone made new transaction to send old queued data too)
Also, in your current scenario, rather than sending single request for each transaction, ask your API Guy to accept request in List of object format. So you just need to create list of request body object and send to server
In my phone when minimize aplicatoin then the Android destroy activity.
In Developer options I turn "Don't keep activities".
I try to implement MVP.
I have a activity with button.
Steps:
User click button
As result activity call method from Presenter: presenter.dowanloadFile(). This is a async http request. The size of file is about 10 MB.
Show progress
After 10 seconds user get success http response
Presenter call method from view: view.hideProgress
OK. This case work fine. Nice.
But suppose the next case:
User click button
As result activity call method from Presenter: presenter.dowanloadFile(). This is a async http request.
Show progress
After 2 seconds user minimize application
As result activity is destroy (because turn Don't keep activities)
After 3 second user return to application
As result create new activity
After 5 seconds user get success http response
Presenter call method from view: view.hideProgress
The question is:
Is I need to continue http request when user minimize application (item 4). Or I must cancel current http request. And when user again return to application I must again start new async http request?
I want the next: When user return to application and if http request success finish then to show result. If the request is not finish then continue to wait http response.
It depends on what you want to happen? It seems like it's wasteful to cancel a request that is expensive (10 seconds is a lot), however you also need to consider what "cancel" mean in the context of an HTTP request.
For example, if cancelling a request, only prevents results from being delivered. Than means that your file was actually uploaded but you don't get callbacks anymore for that request result. Also be careful if your upload thread has a reference to your view, it will be leaked till the upload is done.
If you don't really care about the request, you can just cancel it. Assuming your server is smart enough to identify another request for the same file, so you don't duplicate it if your activity was re-created.
Another option would be not cancelling the request. Then you would need some mechanism of having your "presenter" survive the Activity re-creation.
There are numerous ways of doing that:
Having a cache for your presenters and not re-creating it if it exists in the cache.
Using headless fragments, which are basically are fragments with setRetainInstance(true) to survive an activity re-creation.
Moving your upload logic to a background Service or JobScheduler, and having your presenter/view only subscribe to the state of the upload process, but not actually own it.
You can investigate each option individually when you decide what is the most convenient for your application.
I need to make a http request in my main activity and if the request completes while the user is reading the basic info in this activity, I must store the returned info in a variable. However, if the user clicks in a button in this activity, another activity will be opened. This new activity will continue waiting the same request started from main activity, and display the data when it finishes.
I've read about IntentService, however it can't be aborted (am I right?) and the user can ask for new data before the request is completed.
What alternatives do I have?
PS: The request will only work while the app is running.
PS2: I'm using Volley for http requests.
Sounds like you might want to use a Service instead of an IntnetService, since volley will kick off on a separate thread. You can then handle the management of the volley task in that service.