Since two days I am trying to consume a WCF (.NET) Soap Service and serialize it's response without success. I am getting a correct response (I had to put it on pastebin: SOAP Response Example), but KSOAP2 is not able to handle .NET Datasets correctly.
I already consultated various articles about this specific problem, but none has a .NET Dataset to handle. The main article which gave guideance was an
article by IBM "Working with XML on Android"
I tried following steps to parse my data without success:
Parsing with SAX (android) -> Seems not to work with this complex document because of the different namespaces.
with DOM Object(android) -> NullPointerExeption (dooh!)
with Digester (dom4j) -> NullPointerException (arggh!)
method suggested by helloandroid.com "Using ksoap2 for android, and parsing output data"
Some questions:
- The returned response is a normal XML, but actually it includes a .NET Dataset. Has anyone had success to parse data out of such a response?
- Is there a way to make KSOAP2 not to trying to parse the data? It returns a rubbish SoapObject, which is unreadable. I would like just the contents of the SOAP body. Is there a way to intercept that?
- Do you have any other hint?
With ksoap2 you can set the envelope debugging to true and then get the response dump, which will contain the full xml.
However what makes you think that the SoapObject returned is unreadable. Check it out in a debugger and you will find that everything is there and you can just parse it out using getProperty("propname") and getAttribute("attribute) which in turn are either SoapObjects again if they are nested or contain actual values if they are leaf nodes.
Check out some of the links on the wiki to http://code.google.com/p/ksoap2-android/
I have used DOM and SAX so far to parse XML documents. DOM had a few issues on Android and I had to handle some bugs in the API. SAX seems to be better (and leaner if you only read). I have not used ksoap, but did everything hand crafted. Though I am not sure where the problem with that .NET thing is, I wouldn't see an issue using SAX or DOM. Can you comment why you think SAX won't work because of namespaces?
A.
One of my solutions was: http://vtd-xml.sourceforge.net/
I hope someone will find something more suitable.
I have the same problem. First I used vtd-xml. It was working without problems but it is a bit slow. Now I switched to standard Java SAX (not Android SAX implementation) and it works ok.
Related
If i want connect extensible markup language web services with android application what can i do and what the difference if i use java-script object notation web services with android
IF you want to compare XML and JSON format ( what would be better with android). Please see some points here:
JSON is both more compact and (in my view) more readable - in transmission it can be "faster" simply because less data is transferred.
In parsing, it depends on your parser. A parser turning the code (be it JSON or XML) into a data structure (like a map) may benefit from the strict nature of XML (XML Schemas disambiguate the data structure nicely) - however in JSON the type of an item (String/Number/Nested JSON Object) can be inferred syntactically.
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.
I can think of to use XML over JSON is when your webservice responses are huge. JSON usually requires the entire response to arrive before you can start parsing.
You can reach more example about them on internet, and do some android test to see the performance as well.
Example of JSON parsing in Android you can find in this Restful_Webservice_Call_And_Get_And_Parse_JSON_Data-_Android.
Example of XML parsing in Android you can find in this XML_Parsing_Android.
Hopefully, it would be helpful for you!
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.
I have an android application acessing an webservice that returns a big result.
The return type, on the webservice C# server, is the type XmlElement.
My problem is, when I call it using ksoap2 on the android app, it takes a lot of time to process the result.
As the result is a xml, how can I force the ksoap to give me an String result without process it into a SoapObject?
Or there is another way to process the result more quickly?
Thanks and sorry for my bad english
I guess http get or post and sax xml parser can be used without soap. You will get faster response, smaller app size.
You need to run it in an async task and break it down to ensure the result xml is not too big. The WSDL does not matter since it is not used.
In terms of processing the xml with something else rather than using the parsed SoapObject tree:
If you do that you might as well not use KSAOP2. And you probably wont find anything faster either since KSOAP2 is already very lightweight and fast. You just use a different stack to do the same thing.
I am making an Android app, and I need to connect to a remote database with a webservice.
I will receive an XML file from the webservice with the results of my SELECT query (various rows in some cases).
I don't know much about XML or web services, I only know that I will receive an XML file and that I have to parse it to obtain the data.
Is there an XML parser for this purpose that is easy to add to my app and configure?
I would suggest not using XML and instead use JSON. JSON is much cleaner and much less in kilobytes. Then I would use Jackson to deserialize the JSON string to an object. You really don't need to do any work. Just point to the URL and you get an object back. You just have to make sure on the webservice you also generate correct JSON, which you can also use Jackson.
Here is how easy it is:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
User user = mapper.readValue(new File("user.json"), User.class); // can use File, URL, String!
Designing XML parsers are not very difficult. You can probably google for xml parsers and then change them to look for the tags you need or values.
Google is your friend.
You don't need an in depth knowledge about XML but you should know a bit about it. A recommended read would be this article. You should also at least know how the server is handling the communication. Is it a unidirectional communication (your application only fetching data) or is it a bidirectional communication (you are sending requests to the server). If the later how is the server handling them and so on. A bit of background knowledge is required.
I always recommend using the SAX parser as it seems so be the most efficient one due to its concept (being event based). A good read about dealing with XML files on Android can be found here. And also don't forget to search for similar questions here on StackOverflow as it is quite a popular question with some quality answers.
In the Android application I am building, I want to be able to communicate with a local server developed in Django. (Basically a login page and a home page populated with posts and images from users) So do I need to use XML Parsers for the parsing the response from a Django server or is it possible for the server to respond with strings which can be directly used? Also what about images?
Is the JSON or XML Parser easier and robust to use in Android? The responses would be basically like tweets with a username, image and message. I was thinking of using the SAXParser. Any better alternatives?
Regards,
Primal
Android has built in libraries for parsing both JSON and XML.
In my opinion, the easier (and better) one would probably JSON if you're just looking to output the serialized version of your models.
Some relevant links:
JSON:
https://developer.android.com/reference/org/json/JSONObject.html
XML:
https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
https://developer.android.com/reference/javax/xml/parsers/package-summary.html
Edit: In response to the last part of your question, yes, you can just output strings. Depending on the complexity of your data, you'll end up making things harder for yourself. Parsing JSON on Android is super easy. Just do it.
SAXParser is very easy to use, it just calls a method when it enters a node with the name of the node and its arguments.
So yes, using SAX is a good idea.