Heavy traffic on server causes app to force close - android

I have developed an app to communicate with my own server and published it. However, sometimes the app force closes. I know there is no bug in the code because the app works properly most of the time, but sometimes it is waiting for an answer from the server forever. I think this is due to the fact that so many people are using the app, and the app refreshes every 1 second or so, so this heavy traffic causes the server to take a large amount of time to respond. So how do you take care of such a use case? should I have a use case where if the server does not respond after some time you just stop the app and throw a message saying that the server is not responding or something like that?

Right now, your main application is timing out due to server load. If you put your connection details in a thread, you will be able to avoid having that main thread time out. You can check for updated data from the connection thread (through some shared object) and then present a message to the user if the data has stopped.

It sounds like you have your server communication code within your main Activity. Any code running in this Activity will be run in the main UI thread. If your code sends a request to your server, and is then waiting for a response, the main UI thread is blocked until your server responds. The Android OS recognises that the UI thread has effectively hung, and kills your app.
What you need to do is to separate out the UI code in your Activity from the server communication code. You should move this into an AsyncTask or a new Thread/Handler combination. This will allow the UI to remain responsive even when your server is under load.
Documentation for AsyncTask
Designing for Responsiveness
Android Thread Messaging
Thread example

Related

Sinch - Android , Instant Message History returns in full when user uninstall and install the app

I am using the sinch service to build a chat app in android and I realized that when a user uninstall the app or clear cache and logs in again the full history is sent. If my user has 1300 messages in the history this will be retrieved in full and my UI is getting stuck. I tried to put the SinchMessageListener initialization in a RXjava observable to execute all that in a new thread and its not working. Still the UI got stuck with every call of onMessageSent, onMessageReceived, onMessageDelivered. Something inside the SDK forces to execute the listener in the UI Thread
What can I do about it? Any work around or idea?
When a Sinch client is created, and "internal" callback handler is created that is bound to the Looper of the current thread. So this issue should be resolved if you creates the Sinch client on a background tread. Though that also means you need to keep that thread around (probably for all app / Android service lifetime), and also dispatching all events that will actually trigger any UI update to the main thread
i would filter the messages and dont trigger any ui updates for old messages.
BTW we send the messages for 30 days to a new device.

Manage threads to send large amount of data throught network

What is the best way to send a big file from an Android App to a distant server ?
Since there is one thread for the UI, I would like to keep 1 thread for the network connection (SFTP protocol) without reconnecting at every file I send.
* AsyncTask is bad because the operation will last way too long and the UI will freeze.
* IntentService is not the best since it has to reconnect for every file (when the Thread ends, it auto disconnects and I don't see how to keep the connection open).
Something nice would be : 1 thread for the UI, 1 thread to keep the connection open, 1 other thread for downloading/uploading to the server.
Maybe with a standard Runnable class ?
Thanks in advance !
Services are the right tool, especially as a service can run in the background to perform work even while the user is in a different application.

How to create a chat screen in Android?

I'm creating an Android chat application. I want chat screen to include features like a new UI screen for chat thread with a different user. But I don't know to move further with it. Should I use service or asyntask or is there any other way. Please help me get started.
No. if you use each thread for each user, you will spawn many threads (memory, and time to create and clear...).
You should use a service to listen when receive message (this is an easy work by using intent), and you can do in your main UI Thread.
The chat gets closed (The network connection with the server/another device which the user was chatting with) once the activity closes, so I don't see a reason to use a serivce/AsyncTask. Just have a thread waiting for data from the socket streams, and show that data to the user once it arrives.

Android queue's and threads

I am in the middle of developing an android application and I have stumbled across something that I don't really know the best way to solve.
What I wwant to achieve is, when a user logs into the application, I want to start a thread if the device is connected to a network(what kind of network doesn't matter)
The thread should perform an action every 10 minutes.
What this thread needs to do is, loop trough a list, a queue to be more exact.
This queue will have objects, and based on the objects in the queue when there is a connection available, execute.
The queue will be filled trough the flow of the application.
For example filling in a questionary.
The answers need to be synched to the server. Every question can include pictures takebn from the camera etc, so I want to save certain data as an object, put them in a queue, and have a thread handle the http requests. This way the UI won't be blocked. It's of great importance to sync whenever possible.
What I want to avoid is having another process run aside from my own APP. That's why I haven't used a service. Or do I missunderstand the concept of services as a whole?
Are there specific queue objects or lists?
I want to loop trough the queue list that can be filled at anytime while the program is alive, with a thread.sleep like method when the list is completely empty.
Please leave me hints and tips on what way to go with this.
A service isn't it's own process... from the Documentation: "A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of."
A service really is the best choice for what you're talking about. You spawn your own thread in the service that then does the following: check your queue for objects and send any to the server (since you're already not on the UI thread, you can do this without spawning yet another thread if you want). If the queue is empty, use a Timer to schedule another invocation of your upload method.

Crash Log in Android : Any Idea

I am new in android and working on a maintenance project
Application crashes if no internet connection while creating Soap Request
Following is screenshot of LogCat, Can any body tell me Whats the reason for crashing.
Amit Battan
You are getting a ANR. It stands for Application Not Resoponding.
Android kills applications which are unresponsive to user interactions. Basically when you are doing heavy processing and the user clicks on a button if your application doesn't respond to the click event within 5 seconds.
In your case, if the internet connection is not there I think your application keeps trying to connect and never comes out of this. Hence the ANR. Consider having a timeout for the request..
Check this link for more details on ANR.
ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog.
Any task that will take more time should not be performed in the UI thread, and should be moved to either an AsyncTask or a Thread & Handler.
Check the /data/anr/traces.txt file.
put the all the downloading task in a thread and check that once....
Actually android allocates some time to every process if the process does not complete its action in that particular time then ANR error will come.

Categories

Resources