I want to extract MsgBody from an HTML string using Jsoup of Jsoup.org. In my code below, am I using the correct method to give access to the Java code and decode MyString? Any help greatly appreciated.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim NativeMe As JavaObject
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
If FirstTime Then
NativeMe.InitializeContext
End If
End Sub
Sub ExtractHTML(MyString As String) As String
Try
MyString = NativeMe.RunMethod("decode", Array(MyString))
Catch
Log("LastException: " & LastException)
End Try
Return MyString
End Sub
#if java
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public static String decode(String s){
Document doc = Jsoup.parseBodyFragment(s);
Element body = doc.body();
return body.toString();
}
#end if
Thank you for your response. No, I do not get any error, but I am not getting the extracted MsgBody that I seek. I'm trying to determine if the problem is in the way I have constructed my B4A code below, or with my Java code.
Thank you for your response. I have looked at this library, but thought I would give it a pass and try inline Java with Jsoup from Jsoup.org instead. Have you had good experience using it yourself? If others are getting good results, I should take a second look.
I've used this lib in the past and it was easy and quick. I don't have a computer to test your code with this lib but I think it's a good idea to test it with your html text
Using jSoup with inline java works great and is much easier than using the B4X jsoup library. You can use examples from around the web without changes and you can test your jSoup code in Eclipse before using the in your B4A app.