Android - Broken JSON parsing - android

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.

Related

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

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.

XML or JSON Data to View mapper in Android

I have some sort of simple mobile shopping section inside my app. I get XML or JSON from server and want to put name/description/price/currency/etc to corresponding views. I really don't want to parse all XML myself and prefer to use some syntax like ["products.product.price" -> R.id.priceTextView] to map xml/json data to Android views.
I googled a little bit but failed to find a good way to do this. Do anybody know some lib or maybe Android class doing that?

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