-1

before giving me negative votes for this post please consider these facts....

i have tried many e.gs for parsing information from the internet,

some say that the link above is in json which is made to look like xml,some say its in xml...

<resp stat="ok" version="2.0">
<artist>
<images>
<image height="480" type="primary" uri="http://api.discogs.com/image/A-29735-1232126311.jpeg" uri150="http://api.discogs.com/image/A-150-29735-1232126311.jpeg" width="471"/>
<image height="495" type="secondary" uri="http://api.discogs.com/image/A-29735-1147363179.jpeg" uri150="http://api.discogs.com/image/A-150-29735-1147363179.jpeg" width="574"/>
<image height="480" type="secondary" uri="http://api.discogs.com/image/A-29735-1147363173.jpeg" uri150="http://api.discogs.com/image/A-150-29735-1147363173.jpeg" width="600"/>
<image height="299" type="secondary" uri="http://api.discogs.com/image/A-29735-001.jpg" uri150="http://api.discogs.com/image/A-150-29735-001.jpg" width="299"/>
<image height="298" type="secondary" uri="http://api.discogs.com/image/A-29735-1147363246.jpeg" uri150="http://api.discogs.com/image/A-150-29735-1147363246.jpeg" width="451"/>
<image height="600" type="secondary" uri="http://api.discogs.com/image/A-29735-1259307969.jpeg" uri150="http://api.discogs.com/image/A-150-29735-1259307969.jpeg" width="600"/>
<image height="300" type="secondary" uri="http://api.discogs.com/image/A-29735-1306883944.jpeg" uri150="http://api.discogs.com/image/A-150-29735-1306883944.jpeg" width="300"/>
</images>
<id>29735</id>
<name>Coldplay</name>
<profile>
Coldplay is an English rock band from London, England. They've been a band since January 16, 1998 when they lost a demotape competition on XFM in London. Philip Christopher Harvey is the band's manager. [b][u]Line-up:[/u][/b] Jonny Buckland (Jonathan Mark Buckland) - Guitar Will Champion (William Champion) - Drums Guy Berryman (Guy Rupert Berryman) - Bass Chris Martin (Christopher Anthony John Martin) - Vocals
</profile>
<data_quality>Correct</data_quality>
<urls>
<url>http://www.coldplay.com</url>
<url>http://www.myspace.com/coldplay</url>
<url>http://www.youtube.com/ColdplayTV</url>
<url>http://www.twitter.com/coldplay</url>
<url>http://www.facebook.com/pages/Coldplay/15253175252</url>
<url>http://en.wikipedia.org/wiki/Coldplay</url>
</urls>
<namevariations>
<name>Cold Play</name>
<name>Cold Played</name>
<name>Cold Player, The</name>
<name>Coldplay, The</name>
<name>Goldplay</name>
</namevariations>
<members>
<name>Chris Martin</name>
<name>Guy Berryman</name>
<name>Jon Buckland</name>
<name>Will Champion</name>
</members>
</artist>
</resp>

i don't know myself in what format it is,but looks like xml to me.. i don't remeber how but i was able to get json like information from this page,not from my code but by playing around with that webpage(i didn't use a converter though)

{"resp": {"status": true, "version": "2.0", "artist": {"profile": "An Australian rock band, formed in 1973 by Angus and Malcolm Young, they teamed up with Dave Evans (vocals), Larry Van Kriedt (bass) and Colin Burgess (drums). In 1974 both Larry Van Kriedt and Colin Burgess left and were replaced by Rob Bailey (bass) and Peter Clack (drums), a further change in 1974 saw Peter Clack leave and Tony Currenti (drums) join the band. In June 1974 they were signed by Harry Vanda & George Young (Malcolm & Angus's brother) to Albert Productions. In November 1974, Dave Evans left the band and was replaced by Bon Scott (vocals & bagpipes). Rob Bailey also left in 1974 and was replaced by George Young (bass). In 1975 Phil Rudd (drums) replaced Tony Currenti and Mark Evans (bass) replaced George Young.  In June 1977 Mark Evans left and is replaced by Cliff Williams (bass) for their first tour of the USA. On the 19 Feb 1980 Bon Scott died at the age of 33. Brian Johnson (ex Geordie) joined the band to replace him on vocals and the album \"Back In Black\" was released, a tribute to Bon Scott, this album became the 2nd largest selling album of all time with over 40 million copies sold worldwide. In May 1983, Phil Rudd had a parting of the ways and was replaced by Simon Wright (drums), aged 20 then. November 1989 Simon Wright left and is replaced by Chris Slade (ex Manfred Mann's Earth Band, Uriah Heep & The Firm). In summer 1994 Phil Rudd \"quietly\" rejoined the band.\r\n\r\nAC/DC are Australia's most successful rock band ever, and are popular around the world.\r\nThe band was inducted into Rock And Roll Hall Of Fame in 2003 as a performer.\r\n\r\nCurrent line-up:\r\nAngus Young (lead guitar)\r\nMalcolm Young (rhythm guitar)\r\nBrian Johnson (vocals)\r\nCliff Williams (bass guitar)\r\nPhil Rudd (drums)\n", "releases_url": "http://api.discogs.com/artists/84752/releases", "name": "AC/DC", "uri": "http://www.discogs.com/artist/AC%2FDC", "members": ["Angus Young", "Bon Scott", "Brian Johnson", "Chris Slade", "Cliff Williams", "Colin Burgess", "Dave Evans", "Larry Van Kriedt", "Malcolm Young", "Mark Evans (3)", "Phil Rudd", "Simon Wright (4)"], "urls": ["http://www.acdcrocks.com/", "http://www.acdc.com/", "http://www.acdcpower.net/", "http://www.myspace.com/acdc", "http://en.wikipedia.org/wiki/AC/DC"], "images": [{"uri": "http://api.discogs.com/image/A-84752-1233004620.jpeg", "height": 309, "width": 418, "resource_url": "http://api.discogs.com/image/A-84752-1233004620.jpeg", "type": "primary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004620.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1094915280.jpg", "height": 313, "width": 300, "resource_url": "http://api.discogs.com/image/A-84752-1094915280.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1094915280.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1105107816.jpg", "height": 129, "width": 180, "resource_url": "http://api.discogs.com/image/A-84752-1105107816.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1105107816.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1107645658.jpg", "height": 199, "width": 200, "resource_url": "http://api.discogs.com/image/A-84752-1107645658.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1107645658.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1182165014.jpeg", "height": 335, "width": 498, "resource_url": "http://api.discogs.com/image/A-84752-1182165014.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1182165014.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004627.jpeg", "height": 360, "width": 480, "resource_url": "http://api.discogs.com/image/A-84752-1233004627.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004627.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004633.jpeg", "height": 247, "width": 457, "resource_url": "http://api.discogs.com/image/A-84752-1233004633.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004633.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004641.jpeg", "height": 376, "width": 400, "resource_url": "http://api.discogs.com/image/A-84752-1233004641.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004641.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004648.jpeg", "height": 389, "width": 572, "resource_url": "http://api.discogs.com/image/A-84752-1233004648.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004648.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1297548279.jpeg", "height": 414, "width": 600, "resource_url": "http://api.discogs.com/image/A-84752-1297548279.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1297548279.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1294786426.jpeg", "height": 450, "width": 450, "resource_url": "http://api.discogs.com/image/A-84752-1294786426.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1294786426.jpeg"}], "resource_url": "http://api.discogs.com/artists/84752", "id": 84752, "data_quality": "Correct", "namevariations": ["AC / DC", "AC DC", "AC-DC", "AC//DC", "ACDC", "DC/AC", "\u042d\u0439 \u0421\u0438 \u0414\u0438 \u0421\u0438"]}}}

but when you simply try to click on the link it looks like an xml file.... be it whatever be ...i saved both the formats of it and once tried to fetch xml information and once tried to fetch json information....xml worked for me ..i used the following code,which worked...but the thing is....when i try to fetch the information directly from the link....i don't get anything,please can someone fetch anything from that link?i am totally confused what should i be fetching json or xml?

Document doc = builder.parse("D:/workspace1/dd.xml");/////android_asset/www/dd.xml
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//resp/artist/images/image[@uri]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

for (int zzz = 0; zzz < nl.getLength(); zzz++)
{
    Node currentItem = nl.item(zzz);
    String key = currentItem.getAttributes().getNamedItem("uri").getNodeValue();
    System.out.println(key);
}

also the above code just won't work in android(works fine in java)..(you have to surround it with try/catch to make it error free )

also when i saved it as a json file i tried this code which didn't work

HttpPost httppost = new HttpPost("file://D:/workspace1/dd.json");

httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;

try {
    HttpResponse response = httpclient.execute(httppost);           
    HttpEntity entity = response.getEntity();
    String result=null;
    inputStream = entity.getContent();
    // json is UTF-8 by default
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null)
    {
        sb.append(line + "\n");
    }
    result = sb.toString();
    TV.setText(result);
    Bundle jObject = null;
    String aJsonString = jObject.getString("status");
     TV.setText( aJsonString);
} catch (Exception e) { 
    Log.e("MYAPP", "exception", e);
}
finally {
    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
Community
  • 1
  • 1
Ankit Srivastava
  • 280
  • 2
  • 7
  • 24

1 Answers1

0

Ok, your question is bit confusing basically, the link to me only produces XML, it's pretty standard stuff, although bizarrely If i request say http://api.discogs.com/ I only get JSON data, this doesn't matter even if I make the request with a content type of 'application/xml' and vice versa with the provided url and a content type of 'application/json'.

What you need to do is in your responses from the HTTP requests look at the content type of the body being returned, if it's 'application/xml' use your XML parser, if it's 'application/json' then use a JSON parser.

I can't say much more than this with out documentation of the site about what it's meant to provide. Ideally if you can use all JSON then you should it's better for mobiles to consume over XML.

An example of how to parse JSON for Android is available here: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Your data will obviously be more complex than the example as you have objects inside objects.

The basics though is everything is a key:value which in Java would typically represent a Map.

Peter Fox
  • 1,809
  • 2
  • 20
  • 34
  • well according to the documentation it says its json... you should have read the whole detail of my question ....i tried to fetch information from it considering it as a json as well....but i was unable to. – Ankit Srivastava Aug 22 '13 at 13:26
  • Well you haven't linked to the documentation have you? all you've said is that the site says it's in JSON, which everything is except the Artist objects which are in XML plain and simple. I've also told you, if you have a HTTP response you can check the content type and use the correct parser type accordingly, that's the best you'll get for a poor question I'm afraid. – Peter Fox Aug 22 '13 at 13:35
  • If it is in simple xml why isn't the above code working for me??but working for another xml file ? – Ankit Srivastava Aug 22 '13 at 13:46
  • Well clearly because your example data is in JSON, how you got it in that format I don't know, but as I say, I can't comment on the api as there's no documentation to be read. Opening the link provided it creates it provides XML. If you can't tell the difference between to then I think your biting off more than you can chew. – Peter Fox Aug 22 '13 at 13:57
  • "but when you simply try to click on the link it looks like an xml file.... be it whatever be ...i saved both the formats of it" that means i tried it saving like an xml file as well... ,let me update the question – Ankit Srivastava Aug 22 '13 at 13:59
  • Ok, I looked up the documentation myself, basically you're accessing the URLs wrong... http://www.discogs.com/developers/accessing.html#wrapped-responses You shouldn't be accessing the artist resource via the name, you it seems to be a deprecated way of doing things, instead you should be using id codes. So to get the data in a json format you should be using http://api.discogs.com/artists/84752. The guys who made this API of their's should have applied a way of seperating the versions of the api. – Peter Fox Aug 22 '13 at 14:04
  • No, but using a deprecated API deliberately is a bit dumb when you know it's deprecated, it means that it could be dumped at any point and your app will stop functioning. If you use the /artists/ resource path, yes it will definitely be JSON as the rest of the api resources are. – Peter Fox Aug 22 '13 at 14:22
  • Yeah i am not developing this app for selling it so i don't mind if they dump the api in future – Ankit Srivastava Aug 22 '13 at 14:26
  • Well then, go crazy but half the api will be in XML the other half in JSON then – Peter Fox Aug 22 '13 at 16:59