Scenario :
1. I have a list view, fetching data on the fly. Due to scrolling what data it might get could be stale.
2. When I implemented HTTP client using a thread , on scroll i would cancel all requests threadHandler.removeMessages(intwhat) so that no stale data was returned.
3. The autobahn Websocket API gives me option to sendmessage and receive its response asynchronously.
4. Can anyone advice me on how to go about cancelling the requests from the WebsocketWriter ? or a work around.
5. If the above description is not sufficient , I can elaborate more.
The OPs question has been taken to the Autobahn mailing list here.
Extract:
sendMessage() cannot be canceled. Upon invoking, it'll forward the
message to the background writer thread, and that thread will send out
the bytes on wire.
What you can do (at app level) is ignore any responses to the sent
message the application expects.
Related
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
I am working on JOB queue. Here the scenario is i am storing 3 api calls in job queue. The first API is having more information in json. but last two is having less information. According to the priority when network is available it is calling those apis one bye one in right order. but as the first api is having more information it is taking more time to hit the server so it is reaching the server at last. Is there any way where after hitting the server we can able to run the rest APIs?
To manage your network request one after other you can Use AsyncTask.
AsyncTask have 3 main method i.e
1)onPreExecute - can perform any Ui update while calling network request.
2)doInBackground - which run in background thread not on main thread.
3)onPostExecute - after doinbackground process onPostExecute will call.
like this you can call your request when first request get completed ,So on first onPostExecute , call second request and so On.
I hope like this you can manage your network request in serial way. Thanks :)
Let me first explain my problem:
We are using HTTP REST protocol to transmit chat messages. For chat messages, ordering of messages is critical. Since HTTP is a stateless protocol, we are observing that messages are received out of order on server.
For example, if we send A, B and C - received order on server can be C, B and A.
Is there a way to solve this? I am not sure if HTTP requests can be ordered in Android. Even if we try to do so, that might reduce its efficiency since only single HTTP request will be executing at a time.
Is there any established pattern to solve this?
The reason they arrive in an unpredictable order is because such is the nature of networking. Packets travel through different routing points and you cannot force them to be in order. Even if you send them in order, they are not guaranteed to arrive in order.
But the good news is you can achieve what you are looking for in several different ways.
You can wait for the server to respond back with OK, before you submit the next message. That will ensure the order, but you would lose the multithreading.
A better solution would be to tag each message with an order_id or a timestamp. Then the server puts them in order.
In my case I assigned timestamp on each request and then saved them on my db. Users may see their messages unordered especially when they chat very fast. But when they refresh, the server will return the ordered list based on the timestamp registered.
I wrote an application with behavior like this: "Android client will run a service to send a request HTTP POST to server, server will return data in JSON and delete it from Database".
Problem I got here is: When i start LoadDataActivity (this activity will start service "GetMessage" to receive data froLm server). If i start LoadDataActivity and finish this activity immediately (Service GetMessage will stop too) , that's mean I sent the HTTP POST to server but I have not receive the JSON result yet, while server deleted it in db, so i lost this data.
Is there anyway to help me fix it, but dont have to keep service still running when finish activity?
What you're doing is a bad idea and you need to change some architecture here.
Just because you sent an HTTP request to a server does not mean that the client will get the response. YOu could lose internet connection. A router can break. Someone can trip over a wire and pull it out. The server could crash after deleting the data but before sending it. It's a bad idea.
If you want to do this, you ought to do 1 of 2 things:
1)Have the client send a 2nd request to the server telling it its ok to delete the data now. Do not do it in one request.
2)Use Lazy Deletion. Rather than actually delete the data, add a column "IsDeleted" to the db. Its default should be 0. After sending the data, set it to 1 rather than deleting it. Then change all queries to the table to only return rows where IsDeleted is 0.
Or 3) 1 and 2 combined. This is the best answer.
As for your direct question- no, there's no way to force the HTTP result to come before an activity is finished. So this needs to be done in a service that is started, not bound. An IntentService would work well, so you could have it automatically terminate itself when the request is done, and it would just start a new instance if another request is pending.
I have faced a very weird problem by using Soap object in android. I am running a background thread which will take care of sending data to server periodically. Here i am using Soap web service for sending data. While sending a data to server,Sometime duplicate request(double time sending same data) occurs in server.
How to overcome this issue? Please help me...
Thanks in advance...
Firstly check if there's any loop/condition which is causing the error.
Secondly always within a thread whenever HTTP requests are involved make them synchronized and set a boolean whenever request is fired. Reset the boolean ones the response is received successfully and then fire the next request.
These requests for synchronization can be queued in a list and ones a successful response is received remove the request from list else pop the request out and place according to your need (logically back at the end - so that other requests also get a chance if one is failing repetedly).