SAX parser takes a lot of time to parse - android

I have created an application where I parse data from server using SAX parser. I followed this link
It works fine but it took a lot of time. I need to reduce time taken to parse stuff.
Any pro-tips?

The obvious tip is: profile your code and determine where the time is going. Perhaps you are doing a lot of work handling some of the SAX events. Perhaps the time is being spent doing something entirely unrelated to the parsing. You can't tell until you profile.

Like Ted said, profile your code.
Are you sure its the parse time and not the load time? Are you on wifi or a mobile network? How quickly are other apps loading their data?
That said, don't use that many ArrayList objects, a single array list with a custom container type (i.e. a Ticket object has a price and date, no need to have a price array and date array).
Check you memory usage, usually if things are slow you are generating a ton of garbage. (look for the GC in the log)
Use the final keyword for String parameters that won't change. So setString(final String s) instead of setString(String s). This should prevent the Strings from being duplicated when passed as parameters.
Use JSON instead of XML if you can, its more light weight.
After you've made any significant change, profile your code again

Related

Give me a suggestion to improve my app performance, on retrieving huge data(nearly more than 10,000 records) from server?

On performance view, JSON parsing take huge time for retrieving Data.In my app i need to get nearly 10,000 records from Server.On emulator,it gets data immediately and works efficiently.But in my android phone,it takes more than 2 minutes to retrieve all data.Kindly,give me a suggestion for improve the performance on phone.
The emulator has access to your host machine's resources and is therefore not a good way to test performance.
I have used the Jackson streaming JSON parser with large data sets and it works well for me. However, I run this process in the background and am able to accept long fetch/parse times. Depending on the size of the data and the speed of the device you're running on, 2 minutes does not seem extraordinarily long to me.
Maybe you could fetch a smaller subset of the data first, and then display it while you fetch the rest in the background. You're probably going to have to do some kind of optimization like this in order to improve performance.
I think you can parse the complex JSON response using GSON. Please check these tutorial http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
You just create the model classes and use the proper annotations then the data will be parsed to model objects directly.
The question is, what causes this slowdown. Because of everything goes in the rmulator like charm, it is probably the network speed. You can help this if you find a solution to compress the json data.
It is a text, with a lot of repeat, it is very, very good compressable. And http supports compression.
You need to set it in your http server.
If you find this a promising direction, I suggest to make a new question, giving your http server version. Good luck!

Is it more efficient to use a JSONArray or normal Array for storing/reading data?

I'm working with an app that connects to a PHP/MySQL server from which everything is returned in JSON format. For example, a list of users is returned as a JSONArray of JSONObject. Each object contains the individual user's information (name, location, phone number, etc).
In working with information in this format is is more efficient to leave everything in JSON format and only extract items from the array/objects as needed? Or is it better to extract everything from the JSONArrayand included objects to build a regular Array or ArrayList first?
JSONArray internally uses ArrayList. It's just wrapper over ArrayList. So I'd say there is no difference. JSONObject uses HashMap so again no real drawbacks.
In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects
JSON is smaller than corresponding XML
JSON is faster, i.e. simpler syntax -> easier parsing (faster parsing)
JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.org does a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parse and JSON.stringify when not supported by browsers.
While eval at the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.
JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.
While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.
I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.

Best way to update sqlite on Android

I would like some advice. I'm going to be using an sqlite database that will be pulling down information from my server and then saving it in the DB then displaying it. Could someone advise me of the best way to populate the DB, should I...
Use a http request and return a string de-liminated with say a | and use a loop to write to the data base.
Use a JSON to retrieve the information and then store it in the database.
The information is going to be just text and some fields will contains links to images I want to then download (get to that later). Just wanted some advice on best practices. I have done some searches on SO and other sites but can't find much advice. Also as a side note any examples you know of that are good for noobs :)
Based on what you write here I would pick JSON.
To core points:
JSON is a standard format.
Android ships with a JSON lib (org.json) making it easy to handle it (encode / decode data).
JSON is known by a large community so you can ask questions and get them answered rather easily. With a custom format you cannot tag the question as 'json' here at SO... ;-)
Using standard formats and libraries helps you to avoid designing and implementing this stuff yourself, which makes your software more robust.
Sometime later you might need to add more complex data to your project. By that time it will be rather straightforward to use JSON's array and objects. With your private scheme you will have to add this capability to it and extend your parsing code. That can easily introduce subtle bugs. Or you might decide at that point that it is too hard with your custom format and decide to move over to a standard like JSON, XML, etc. At that time it costs you much more to shift over than if you start with a standard format. Consider time invested to write and test the current code and then the extra time to change to the standard format for the current system.

Handling large XML files in Android

I'm new to Android development and want to create a Google map app. I have to use 2 large XML files with geo data (latlons), 1 is 2mb and the other 8 mb big.
What is the fastest way to access and parse this data? Is it maybe beter to store this into a database? A webservice is not a good idea I think, because everytime the app will start I have to load 10 mb... And that will process very slow I guess...
Has anybody some good advice?
Thanks!
Keep the whole data in the server and access it from device using webservices. Better to go for SAX parser as the data is too large. If possible better to implement search functionality from server end or to implement pagination from server end.
Maybe you can create some kind of meta data to go along with the large xml file. This meta data can point to specific parts of the large xml file. And each small part can be a dom object instead of a sax parser.
I suggest using sax, creating a summary of the data you need in some kind of hashtable (simple key value pairs) and using it. May be using SharedPreferences for data that does not change.
Also you need to think on how / what data will be used and how frequently. May be you can keep a producer consumer architecture, where a thread prepares the data for use, and the consumer uses it when its ready on a notify or something.
You need to think from 3 angles
Not too much serial processing
Not redoing what you have already parsed
Parsing using the right quantum of data. Infact think of dom for small parts to make your design easier to start with.
And I dont recommend DOM for the entire tree. You need to use a combination of SAX and DOM.
First get it working, and then record response/performance delays and work your way up to improve it from the worst to best.

Efficiency questions

I have to manage XML documents and Strings in my app.
In terms of efficiency and memory usage, will a collection like ArrayList be much more expensive than String[]? Also, I could store the content as a regular String or XML. Is working with XML also more expensive? (When I say expensive, I am referring to the use of system resources.)
Those Strings will include xml's.. all i gonna do is pass them to another appz and those appz will deal with them.. nothing more..the ArrayList will hold dynamiclly around 20 Strings each.. and ill will need to 'for each' it, get the content of the string and send it to another app.
If there are differences, are they significant?
Thanks,
Ray.
It depends very much on what you're doing with the strings and how you're using them.
However, from my recent experience using an ArrayList of strings for processing quickly and repeatedly, I found a String[] array 8-10 times as fast for my purposes (read string and compare in a while loop).
It seemed really odd (and annoying because I wanted dynamic size), but the execution time spoke for itself.
Like I say, this is from my own experience recently and I'm just being general. Perhaps if you provide more information on what you're trying to achieve, we may give you more specific advice.

Categories

Resources