Android Question Extracting MsgBody from HTML string with Jsoup

William Hunter

Active Member
Licensed User
Longtime User
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

Best regards :)
 

DonManfred

Expert
Licensed User
Longtime User
Do you get any error?
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Do you get any error?
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.

Best regards
B4X:
MyString = NativeMe.RunMethod("decode", Array(MyString))
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
I need jsoup too and I am not keen on using the B4X library. As you, I would much prefer inline java as I have used jsoup before.

When I compile your example I get an error: package org.jsoup does not exist.

Where do you put the jsoup library in order for this example to work?

UPDATE: I found the answer. The library just have to be in the normal library folder, but I hadn't addressed the library with

B4X:
#AdditionalJar: jsoup-1.10.3
 
Last edited:
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
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.
 
Upvote 0
Top