4

I'm using the Last.fm Java library that's listed in the "Downloads" section on the API. When I call "getWikiText()" and set it to my TextView, all the data is returned; however, the html isn't formatted correctly. Rather than showing separate paragraphs, the data is shown as one large text. To illustrate what I mean, here is a picture of what is being returned and then a link to the artist page of how things should look.

Picture - https://i.stack.imgur.com/xlbla.jpg

How things should look - http://www.last.fm/music/Bon+Iver

This is what I'm calling to get the artist info, but what am I missing as to the paragraph spacing? I've search around quite a bit, but there's not much information on this in particular. I'm basically shooting in the dark at this point, so I need a little help.

artistInfo = Artist.getInfo("Bon Iver",key);
artistText = artistInfo.getWikiText();
info.setText("");
info.append(Html.fromHtml((artistText)));
adneal
  • 30,484
  • 10
  • 122
  • 151
  • Do they provide you with any sort of delimiter for lines (carriage returns or newlines) or is it just plain text and spaces? – Matthew Feb 05 '12 at 15:13
  • Not that I'm aware of. The library is helping me parse the XML you retrieve from Last.fm. [Here's an example of the XML being parsed.](http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Bon+Iver&api_key=b25b959554ed76058ac220b7b2e0a026) (scroll to the bottom and look at the "content" tag.) I tried creating a String from the content tag this morning, I just pasted everything over, but I ended up with the same result. I know the data can be returned like I want it to be, I've seen it done. I'm not sure what I'm missing at this point. – adneal Feb 05 '12 at 19:37
  • There are carriage returns and line feeds all up in that XML. It looks like they are in appropriate spots for paragraphs. – Matthew Feb 05 '12 at 20:39
  • So, do you know of a way to capture them? I was under the impression that using `Html.fromHtml()` would naturally recognize them and space things accordingly, but it seems something more is needed. – adneal Feb 05 '12 at 21:06
  • Well, I would do it with a StringTokenizer - I believe using "\r\n" would match. Maybe there exists a better way if you are using Android libraries. I'm not exactly sure. – Matthew Feb 05 '12 at 22:56

1 Answers1

1

Do a simple replaceAll() call is enough:

String artistText = artistInfo.getWikiText();
artistText = artistText.replaceAll("\n", "<br />");
info.setText(Html.fromHtml(artistText));

Example result

ariefbayu
  • 21,849
  • 12
  • 71
  • 92
  • I called `artistInfo.getWikiText().replaceAll("\n", "
    ");` and the content remained the same. I also called it in `Html.fromHtml()` and in `setText()`, still no change. I also tried what you edited, still nothing. The actual content remains unchanged. The Creative Commons license at the end of the text is breaking though.
    – adneal Feb 07 '12 at 06:33
  • look at the attached code I gave you of how to do `replaceAll()`. I've tested it and it worked! – ariefbayu Feb 07 '12 at 06:39
  • Right, I did. I've used it exactly as you've demonstrated, but the result is still the same. – adneal Feb 07 '12 at 06:41
  • don't do `info.append()`. Do the `info.setText(Html.fromHtml(artistText));` – ariefbayu Feb 07 '12 at 06:42
  • Okay, I see the screen shot. That's weird that mine isn't changing. Then I'll have to try and see what makes my app different from your test app. – adneal Feb 07 '12 at 06:43
  • So, I copied your code to be sure I wan't messing up, still didn't work. Then I built your project altogether and tried it, guess what? [http://i.imgur.com/vd2gW.png](http://i.imgur.com/vd2gW.png) Do you think Ice Cream Sandwich is the culprit here? – adneal Feb 07 '12 at 06:55
  • hmm... not sure. I'll try to download ICS sdk and test it. – ariefbayu Feb 07 '12 at 07:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7408/discussion-between-silent-and-aneal) – ariefbayu Feb 07 '12 at 07:04
  • Yeah, it is. I just tested it on a Gingerbread emulator and it worked like it should. Well, I have no idea how to fix this if it's ICS giving me trouble. – adneal Feb 07 '12 at 07:05
  • Is there any chance Ice Cream Sandwich is disregarding your BR tags? Perhaps you could wrap the text in P tags and see if that works. – Matthew Feb 08 '12 at 11:17