How to skip namespaces from JSON that was converted from XML? - android

This question was first titled: How to remove -xmlnl tags when I receive JSON from a XML-HTTP-API server? but now I understand the concept of the problem.
This question is about a exception in Android regarding JSON translation. I do a HTTP GET request to a (not-mine) webserver made with c# and some api that, when I call it from a browser, the answer comes in XML and when I call it from my Android application the result is in JSON.
The thing is I am getting an JSON error like: Value {"-xmlnl:d4p1":"http://schemas..."} of Alternativas of type org.json.JSONObject cannot be converted to JSONArray
The code in XML has this king of content:
<Alternativas xmlns:d4p1="http://schemas.datacontract.org/2004/07/PainelMonitor.Model"/>
<Descricao>A primeira fraze que vocĂȘ ouviu hoje foi?</Descricao>
I am only interested in the <Descricao> content.
This {"-xmlnl:d4p1":"http://schemas..."} should not come to JSON and it maked my app crash.
So how could I take it off from my JSON? There are 'tens' of these invalid markups in the code, so I can't do it manually because it is a Array of JSON.

In your xsl you have to use **
exclude-result-prefixes=""
** attribute.

Related

Android - Broken JSON parsing

I'm using JSON for some Android Application. I am trying to store links in JSON but they are getting broken inside JSON like below :
11-21 01:15:52.080: D/output(3775): "video_url": "http:\/\/love.com\/bunny.mp4",
I'm putting data like below :
jsonObject.put("thumb", "http://love.com//poster.jpg");
jsonObject.put("url", "http://love.com/bunny.mp4");
jsonObject.put("name", "Bunny");
jsonArray.put(jsonObject);
Anybody has any ideas about this problem?
The strings in JSON are getting escaped. You can either write a simple function to unescape them yourself or use a library which has built in functions like GSON or Jackson.
I personally used this library in the past
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringEscapeUtils.html
It provides a function: unescapeJson(String) which will give you your original links back. Just include it in your project and call the method.

Get Property from XML response sended by Web Service

Accessing web service by Android application I usually use the Ksoap2 library and the code:
response.getProperty("field")
where "field" is the element name of XML, and response is the soapObject in which I have "captured" the WS answer.
The typical XML associate to this answer is:
<registry>
<cf>issjdeodk1292983ls</cf>
<name>John</name>
<surname>Doe</surname>
<sex>M</sex>
<message>a simple optional message</message>
</registry>
In this case when I try to access the field "message" there are no problems:
response.getProperty("message")
returns exactly the string "a simple optional message"
But my web service, if the message is not found (server side) return the following XML file
<registry>
<cf>issjdeodk1292983ls</cf>
<name>John</name>
<surname>Doe</surname>
<sex>M</sex>
</registry>
that is, the same previous XML without tag.
In this case if I use the code:
response.getProperty("message")
an error occurs.
Can I use a mechanism for getting the property only if exists?
I am not sure which library you use but when using xmlpullparser you can lookup if there is a tag like that by
if (parser.getName() == "message") { ....

Ksoap for android not able to create the good soapEnvelop

The soap that I want is like this:
<int:userId>......</int:userId>
This is tested in SoapUI and with this I get the proper response back.
What Ksoap makes is this:
<userId i:type="n0:undefined" xmlns:n0="http://namespace.com">.....</userId>
If I where to paste this code into SoapUI I get an error
'<Message>Error in line 5 position 88. Element 'http://namespace.com:
userId'
contains data of the 'http://namespace.com:undefined' data contract.
The deserializer has no knowledge of any type that maps to this contract.
Add the type corresponding to 'undefined' to the list of known types -
for example, by using the KnownTypeAttribute attribute or by
adding it to the list of known types passed to DataContractSerializer.
</Message>
Maybe I am looking in the wrong direction for the solution but I am now asking how can I make ksoap omit the i:type part. If anyone can provide a different sollution, you will have my thank.
KSoap does have a tendancy to insert its own namespaces into each tag, which can give the output you're seeing.
I had a similar such problem, which was easily resolved using the following :
envelope.implicitTypes = true;
Please also have a look at this other question on SO.

Xml parsing in android

In android app, i want to send a php requeset, and am expecting a response in the form of xml, from the server.
Can anybody provide pointers in this case.
You can use XmlPullParser bundled with android to parse the response. To get the parser instance use this snippet:
XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
pullParser.setInput(...); //Set input source with response you parsing.
There are 3 methods for parsing XMl in android. See working with xml in android.
You may want to try out my XmlParsing library.
https://github.com/frenchie4111/XmlParser
Let me know if you have any problems with it.
Xstream is your friend http://x-stream.github.io/
XStream is a simple library to serialize objects to XML and back again.

Getting Json data into a list view

I have been searching for a way to get the json data from a URl (for example: http://search.twitter.com/trends.json) and display it in a listview. Couldnt get a perfect example to get it done. Can anyone plz help me out by getting the solution and providing a good example of how to do it...
found this tutorial : http://damonsk.com/2010/01/jsonarray-httpclient-android/
Android comes with Json-lib so parsing JSON is easy. You just need to use the HTTPclient classes to retrieve the data from the URL, and you can use the JSONObject class to parse the JSON. And write a ListAdapter to pull data from the JSONObject.

Categories

Resources