json parsing during download - android

Is it possible to parse json in parts when it was still downloaded?
For example I load a long json array with a very slow Internet. Can I download a part of text, parse it, add to screen, download next part and so on?

Check out the following SO question it has a few answers, but generally the consensus seems to be that you can depending on the package you are using. I have used GSON (Mentioned in the answers) but have never tried the incremental streaming feature.
GSON makes a lot of things much easier in general though, so I would check that out first if the other things GSON offers seem like a plus to whatever you are trying to do.

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!

which is more efficient parsing xml or json in android app

I would like to know which is more efficient to get the data from the server by the xml or json.
Another question:
does XmlPullParser related to parsing xml data that come from the web service? so if I am using json I don't need XmlPullParser ! or there is other uses !
thank you very much
What I've found extremely useful for parsing JSON is Google's gson library. For xml, you can use gson underneath to do the same thing with gson-xml. With a single line of code you can map your JSON/XML to your objects without having to write a single line of parsing code.
If you find performance to be an issue (I'm making this suggestion because these libs make you super productive), there are mechanisms in both to allow you finer grained control. I doubt you'll have problems with performance though.
For a very thoroughly researched answer to the headline question (though focussed on browsers, not android apps), see David Lee's Balisage 2013 paper:
http://www.balisage.net/Proceedings/vol10/html/Lee01/BalisageVol10-Lee01.html
His conclusion, in one line, is that the choice between XML and JSON makes very little difference in itself - though the details of how you do XML or how you do JSON can make a big difference.

Parsing "ANY" object from/to JSON in *native* Android

I've read dozens of post (here and other places) asking for parsing from/to JSON and most people code their exclusive parsers for each class. The usual answer is to use GSON or other libraries but since android supports JSON natively I don't see the point of use third-party libraries.
So I made my own generic JSONParser using Android JDK libraries exclusively. It works fine for any class whose fields are: primitive types, strings, one-dimensional arrays, or other classes. The problem now is to handle arrays of more dimensions. I'm a noob using reflection and JSON, so maybe I misconceived something.
You have to specify the Class you want to convert the JSON string. Then it basically iterates through all the fields in the class and search for a field in JSON with the same name. This works only for public methods (it's not a hard problem for me now, but if someone have an idea to improve it I'll appreciate as well).
My problem comes when I have an array of arrays, I've tried several ways but I'm not able to find a solution to deal with n-dimensional arrays. This is my method for arrays, the "TO-DO" part is the one I don't have a clue for do it. I've tried using the commented line, but it fails. Possibly it's the same problem in both directions, but I'm concerned now in decoding the JSON.
Everything solved!! Go to comments
I think it could be useful for more people, so I've uploaded the whole code here. http://pastebin.com/X7CmaxNf
The method insertArrayFromJSON() is called from public static Object populateObjectFromJSON(Class classname, JSONObject js) {} line 65th.
I've finally solved the problem by myself. I was stucked with this since yesterday because I was facing wrongly the array problem, but then I changed the method to attack it recursively in the right way. In the pastebin is the whole code ( I don't post here cause it's two hundred lines). Feel free for use it and if someone have some ideas to improve it, please tell me!.
http://pastebin.com/Jtf2SLDu
The main point was using Array.set(), Array.newInstance() and Array.get() methods properly. This is in lines 110-125 if someone wants to take a look.
For gain access to the protected&private methods I found the solution in this webiste. So now it works for any class with any public/protected/private field. http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html
I've made in a way that you can set the privacy desired before parsing.

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.

Do you advise clients to use JSON or XML? And Why?

Many times my client ask me whether they will deliver data via XML feed or JSON strings. I usually say:
XML if you already have a feed and do not have a web developer who will create script for generating JSON strings
JSON if you do not have any feed and need to create any from the scratch
What do you say? Do you think that delivering data via XML feeds is obsolete and that XML is over-complicated and too heavy?
Should I advise all clients (for the sake of the future) to move onto JSON way of delivering data?
EDIT
From another discussion https://stackoverflow.com/questions/2636245/choosing-between-json-and-xml I can see that JSON is advised for web services, which is the most used case scenario in my clients. It seems that I was advising them properly.
What is they want to pass news articles onto a mobile device - shall I advise XML of JSON?
What about post&get cases when I need to post some data and the to get the response which will be displayed on user's mobile device - XML or JSON?
If the consumers are browsers or mobile devices, I would recommend JSON.
Faster
Lighter
Native parsing support
If the consumers are other programs, I would recommend XML
Can be validated easily
Code generators available to make programming easy and is less error-prone
JSON - if you have a choice :) Google GSON is a serious help there.
We Use JSON: If we want to serialize a data structure that’s not too text-heavy and all you want is for the receiver to get the same data structure with minimal effort
We use XML:If we want to provide general-purpose data that the receiver might want to do unforeseen weird and crazy things with, or if you want to be really paranoid and picky about i18n, or if what you’re sending is more like a document than a struct, or if the order of the data matters, or if the data is potentially long-lived.
This discussed topic might help you .
I agree with all the other recommendations for JSON, but for me the main reason for going with JSON is it's far easier to process on the server especially if you are using a language that supports the JSON structure natively (e.g NodeJS or Python).
I would not say XML is obsolete though. The one obvious case where XML wins is readability. As a programmer I would say JSON is just as readable but I've worked with a lot of people (mainly web designer types) who prefer the look and feel of XML, probably because they are already intimately familiar with HTML.
I agree with your assessment really. Json is easier (for a human) to read, more intuitive and lightweight. XML is better if you have lots of existing XML solutions/interfaces that you're plugging in to. I see XML as the established, mature heavyweight of structured documents, but you don't always need an established, mature heavyweight. It all depends on the use case.

Categories

Resources