【问题标题】:ANDROID:How to fetch information from this link http://api.discogs.com/artist/ac/dc?ANDROID:如何从这个链接 http://api.discogs.com/artist/ac/dc 获取信息?
【发布时间】:2013-08-25 04:32:52
【问题描述】:

在给我投反对票之前,请考虑这些事实......

我已经尝试了很多例如解析来自互联网的信息,

有人说上面的链接是 json 格式的,看起来像 xml,有人说它是 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>

我不知道它是什么格式,但对我来说看起来像 xml.. 我不记得如何,但我能够从这个页面获得类似 json 的信息,而不是从我的代码中,而是通过玩那个网页(虽然我没有使用转换器)

{"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"]}}}

但是当您尝试单击链接时,它看起来像一个 xml 文件.... 不管是什么...我都保存了它的两种格式,曾经尝试获取 xml 信息,曾经尝试获取 json 信息..xml 对我有用..我使用了以下代码,它有效...但是问题是....当我尝试直接从链接中获取信息时....我什么也没得到,请问有人可以从该链接中获取任何东西吗?我完全困惑我应该获取什么 json 或 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);
}

上面的代码也不能在 android 中工作(在 java 中工作正常)..(你必须用 try/catch 包围它以使其无错误)

当我将它保存为 json 文件时,我尝试了这段代码,但它不起作用

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){}
}

【问题讨论】:

  • 我昨天开始使用 Discogs API,到目前为止,我一直在访问将它作为 JSON 的所有内容(如果我愿意的话,JSONP)。我正在使用 ASP.NET / C# / MVC 3 / Razor / jQuery。
  • 所以它在 json 中对吗?你能详细说明一下吗?因为我现在完全糊涂了,这个解析的东西对我来说是全新的
  • 您可以使用 jQuery:api.jquery.com/jQuery.parseJSON 或本机 Javascript:stackoverflow.com/questions/4935632/…
  • 但我不想为 android 应用程序这样做......它会被支持吗?
  • 那么抱歉,我帮不了你。

标签: android json xml-parsing html-parsing android-music-player


【解决方案1】:

好吧,你的问题基本上有点令人困惑,我的链接只产生 XML,这是非常标准的东西,虽然奇怪的是如果我要求说 http://api.discogs.com/ 我只得到 JSON 数据,即使我制作了也没关系请求内容类型为“application/xml”,反之亦然,提供的 url 和内容类型为“application/json”。

您需要在来自 HTTP 请求的响应中查看返回的正文的内容类型,如果是“应用程序/xml”,请使用您的 XML 解析器,如果是“应用程序/json”,则使用 JSON解析器。

如果没有关于该网站所提供内容的文档,我只能说更多。理想情况下,如果您可以使用所有 JSON,那么您最好让手机使用 XML。

此处提供了如何为 Android 解析 JSON 的示例:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

您的数据显然会比示例更复杂,因为对象中有对象。

不过,最基本的是一切都是键:值,在 Java 中它通常代表一个 Map。

【讨论】:

  • 好吧,根据它说它的json的文档......你应该已经阅读了我的问题的全部细节......我试图从中获取信息,考虑到它也是一个json...... ..但我做不到。
  • 你还没有链接到文档对吧?您所说的只是该网站说它是 JSON 格式,除了 Artist 对象之外的一切都是 XML 简单明了的。我还告诉过你,如果你有一个 HTTP 响应,你可以检查内容类型并相应地使用正确的解析器类型,恐怕这是一个糟糕的问题你会得到的最好的结果。
  • 如果是简单的 xml,为什么上面的代码对我不起作用?但适用于另一个 xml 文件?
  • 很明显,因为您的示例数据是 JSON 格式,我不知道您是如何以这种格式获得它的,但正如我所说,我无法评论 api,因为没有文档读。打开它创建的链接提供了 XML。如果您无法区分两者之间的区别,那么我认为您咬的比咀嚼的多。
  • "但是当您尝试单击链接时,它看起来像一个 xml 文件......不管是什么......我都保存了它的两种格式" 这意味着我尝试保存像xml文件一样...,让我更新问题
猜你喜欢
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
相关资源
最近更新 更多