Traversing String html

Asked

Viewed 80 times

0

Good afternoon!

I’m getting an html string to display in a webview.

Inside that string may or may not be images, but for me to display it, I have to pass a base URL before src= "crmResources/, example: "http://desenv.ex.com.br/crmResources/..."

Then I have to save the whole String until I find an image, save it in another variable concatenating with a Urlbases variable (example: http://desenv.ex.com.br/), see if there are more images to do the same job and finally concatenate everything into a String to display in webview.

What would be the best way or logic to do that? Remembering that images may or may not exist, and between them there may be other html elements.

  • The html string you get, is it like the whole page? If yes, you could use regular expressions to replace src=" with the image address

  • This would actually be the content, paragraphs, subheadings, images, lists and etc

  • Have you tried with replaceAll? variableString.replaceAll("<img src="", "<img src="http://addressees/");

  • Thanks Alisson, it worked great, I had some problems with the , but it worked

  • Please, thank you for helping. :)

1 answer

1


If at all times you are using way relative, as in your example, and based on this address http, you can use the method loadDataWithBaseURL. It goes like this:

webView.loadDataWithBaseURL("http://desenv.ex.com.br", strHtml, "text/html", "utf-8", "about:blank");

With that, your WebView will be based on this URL, and all relative path within this string shall have it as a reference.

There is no need to go through all the html. Remembering that you need to have permission in your manifest for internet access.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.