How to send JSON data - android

I have quite much data to send to a server. The format is JSON and my platform is Android. I was wondering would it be wise to somehow divide the data in smaller packets, or send all the data at once? Also would it be good idea to run the sending code in a different thread? I use HTTPPost to send the data with Android

Creating smaller packets will cause a lot more overhead, thus a lot more data to send. Also, all networking should be executed on a separate thread (not UI thread).

Whether to use "many" HTTP requests or just one depends on your program flow. On one hand, you don't want to send data over the network that you are not sure you will need, so one catch-EVERYTHING post may be a bad idea. On the other, reducing the number of requests is going to reduce the overhead associated with each one, and thus result in less total time spent sending.
And yes, always perform network operations outside the UI thread. As far as the rest of the system is concerned, these are intolerably slow.

Related

What is the best practice for a multiple request at same time

I have to send four different request in an api at the same time. Do i need to make AsyncTask background thread for each request or all request could be done through a single AsyncTask. Can somebody please help.
This is a concurrency issue. There is literally dozens of ways to do this in Android. I've written almost every single one for courses that cover this material... and even then it isn't 'simple'.
I'd personally make use of HaMeR (Handler, Messages, Runnable) framework of Android. Create 4 runnables and have them post their results to a Handler.
However... That isn't the easiest to implement. and would require you to understand how to safely create your own custom handler (making use of WeakReference properly, etc.)
Therefore, I'd recommend running the asyncTask(s) on the executorService
myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); The default thread pool executor should start with 4 threads (I believe off-hand without looking it up).
I am assuming you are using HttpURLConnections. Unfortunately each of those connections, as specified in the documentation, is capable of handling only a single request.
However, you can (and possibly should) still perform all your requests in a single AsyncTask. Each AsyncTask will require the creation of a new thread which takes lots of time and resources. So don't listen to anyone who tells you to create a new task for each request.
You also have the option of exploiting HTTP persistence. If you add the header Connection: Keep-Alive to your request via connection.setRequestProperty("Connection", "Keep-Alive");, you will be able to send multiple requests over the same connection and save a lot of time and resources.
It's a little complicated in Java, because of the one-request-per-httpurlconnection rule, but it can be done. First, when you are done with your first request's HttpURLConnection do not close that connection. Then, to create the next connection, call url.openConnection() on the same URL object that you used to create your first HttpURLConnection. The JVM will know to reuse that connection if possible to save bandwidth.
You also have the option of using HTTP/2.0 multiplexing, which allows you to send multiple requests literally at the same time. Unfortunately I am not yet well versed enough in HTTP/2.0 to tell you exactly how to make use of this, but the multiplexing feature was included to solve exactly this problem.

Options on Android for real time sending of data to a server?

I am currently working on a small scale project to prove something works, I currently have a smart band device which has an Android SDK.
From this device I use the SDK to track a users heart rate in real time.
So my Android application receives updates to the heart rate in real time.
This was fairly easy to do however I now need to send this data in real time from the Android device to the server as efficiently as possible.
To start with battery drain is OK as initially this is just a proof of concept.
I have limited experience with sending large amounts of data to a server in real time and I was wondering if anyone has an ideas on what might be the best approach on Android?
I have looked into Sync Adapters but these seem to be more about keeping data aligned between the client and the server, this is something I am not concerned about. Another approach would be to see if the RequestQueue from Volley might work but again I am unsure if looking into this is even worthwhile?
Should I be looking into creating a Service and somehow using a socket to transfer the data?
EDIT: It looks like IntentService may be the best option for handling the task execution but I am assuming http requests would be too heavy for the client and I should look into something else for the transfer?
I am working on similar kind of project but the wrist band I am dealing with is Empatica E4. Please bear in mind that I am not an expert developer, therefore I am also looking forward for corrections in my design. Also, I will try to justify my idea step-by-step and as much as I can. I hope that this will give you some hints for your application and help others as well.
So, my current architecture looks like;
First of all, Empatica has also provided an Android SDK to receive the data. SF stands for Sampling Frequency whereas EDA, Temp, BVP and AccXYZ are my sensors in wrist band. Each sensor has different sampling frequency and the maximum is 64 Hz which gives you 15 ms interval between each samples. This interval is quite challenging to perform all the operations, therefore I buffer the sensors data in (Volatile LinkedBlockingQueue) FIFO queue so that I don't miss any sample. This was all happening in the service of my application.
Now, I have a Runnable task that I have used with ScheduledExecutorService to collects samples from the queue with the interval of 250 ms (you can vary it as per your need but I used 250 ms considering my needs, network latency and the device performance) and put them in a single JSON object. Number of samples that this Runnable task collects are varying for each sensor, which are, BVP: 16 samples, AccXYZ: 8 samples, Temp: 1 sample and EDA: 1 sample. At the output, I have a JSON object with the data to be sent to my server.
For transferring data to my server, I am using HTTP POST request. The reasons are easy, fast, efficient and good for concurrency. I am using Volley framework which will handle all my network related issues by itself. So I just add the JSON object in the Volley RequestQueue and my client is done here. As you mentioned, you can use socket connection to achieve your goal but I have to use multiple devices, therefore in my case, sockets can be problematic to achieve concurrency. I also tried to do it manually by using HttpURLConnection but the code was becoming tedious and hard to handle.
Finally, I have a REST API (in Python) on my server side which will handle the POST requests, parse the data and insert it in my MySQL database. As of now, I am still working on this REST API to parse the data and store it in DB. However, I have tested my application and I am successfully receiving the data from my device to the server.
Regarding your question "Should I be looking into creating a Service and somehow using a socket to transfer the data?", it's an excellent option if you are working on a single device. If more than one device, i think Http is better option.
Regarding your second question, I don't think Http would be heavy for the client and Volley is taking all your pain on itself. You just have to make a request queue and voila !!! You can find plenty of good tutorials for volley and I particularly followed this.
I hope my answer will help you a little.
PS: As I am still working on this thing and I have not come up with the final product yet, therefore I cannot surely tell about the risks involved in it but I will keep you updated if something new happens. Also, I am open to any suggestions and ideas that can help. Lastly, the picture above is not very detailed, I made it for you, just to share how I am dealing with the same idea.

does using handler might speed up the data display?

in the App i am developing, i have a a layout with 6 TextViews, these textviews wil display data received at very high rate from a server. so far, i set the
values i receive to the textviews, but when i run the App. the data that textview displays are changing very slow because of the values sent at very high rate.
for an example, the server send almost 100 line in one second, and the datat in this 100 lines should be displayed by the TextViews..but because, as you noticed,
the high rate at which the dtat is being sent, the TextViews cannot show all the data by the time they sent.
What i want to do is, to be able to display these frequent data using the textViews but without lagging or delay in display the data, something like how do you see
the sensor data displayedin a textView.
given the above issue, does using the handler might help?
given the above issue, does using the handler might help?
To answer your question, this is what the docs say in regards to Handler:
There are two main uses for a Handler:
(1) to schedule messages and runnables to be executed as some point in the future; and
(2) to enqueue an action to be performed on a different thread than your own.
Honnestly, I don't see how the Handler could help you achieve what you are looking for.
Do you make your http request in a separate thread? (if not, then you should)
Also, I am not sure I understood the connection between the TextViews and the very high rate from server.
The handler is only a means of enqueuing a Runnable to be run in the Main Thread. You shouldn't plug the output of the server directly to the GUI, because you might be clogging up the main thread.
If you are downloading at high frequency, I'd suggest storing the downloaded data somewhere in memory (or persistence if needed), and have the GUI read this intermediate data at a slower pace.

How to pase large amount of data with out ANR in android?

My Json service has 2700 records,I want to show these records in my application.
When I parse the data it takes 8min to parse. So my application become non responsive.
How to parse the data in segments wise?
Please suggest me the best way.
Regards,
Sravanya
An IntentService would be adapted for this kind of long computations. 8min. is not an acceptable duration for an AsyncTask, since the user/Android system is very likely to kill your application before it is done processing. Hence the use of an external service.
But you are definitely doing something wrong here: fetching 2700 records is not a good idea (too much network usage) and parsing them isn't either if it takes this much time (too much battery usage). You have to limit what you are fetching to only the data you need, and maybe use several requests to fetch just what you need when you need it.
Use an AsyncTask this will let you process the data using a separate thread to the main UI. When processing has finished you can fire a broadcast listener or use onPostExecute to update the UI with the results.
8 mins is a long time though, consider if possible reducing the size of the data requested.

Implementing asynchronous http requests on Android with AsyncTask

I'm building this client for a web service.
Pretty much everything makes requests to a server and now what I do is, I open a new thread and put all my requests in the same thread. That means I'm making all my requests in a serial way inside the thread and that turns into a lot of waiting for the user. Aiming to make the application faster, I want to make every server request in an asynchronous way.
I have a Networking class that handles all the HTTP requests I need and I'm thinking of making it so that every request starts its own thread.
I'm thinking of using ASyncTask for this but I noticed that with ASyncTask I'd need a class for each of my http requests (a class for GET, POST, PUT, etc). Is that the best way of doing it? is there a more efficient/clean way of doing this? What do you guys suggest.
Seems like a design decision that will depend on exactly what you are up to. There are various ways in Android to execute tasks depending on whether the user is waiting for some data or is being notified later on once the background task completes.
I would suggest you to look at this post that compares various task mechanisms in Android. Apart from this also go through the java.util.concurrent package.
I'm sorry this is not a concrete answer, but take it from me - it mostly depends on how are you trying to serve the user. So one can only suggest ideas. Hope this helps.

Categories

Resources