Android Question trying to run the jsoup example

DanteS

Member
Licensed User
Longtime User
I am trying to run the jsoup example, but after a lot of work, I am unable to run it on the device. The compilation and installation run fine and on the PC I can see at the end of the process:
...
Installing file to the device. (0.05s)
Installing with B4A-Bridge.
Successfully completed


but when I click the "Open" button on my cellular I get an error:

"The application did not install because the package appears to be invalid."


I suspect the jsoup-1.8.1.jar library, because I could not reference it in the project, because it does not appear in the list of libraries of the project, although it does appear in the Libraries folder, but only the .jar file. The html does not exist.

Could you give me ideas to solve it?
 

DanteS

Member
Licensed User
Longtime User
Hello Mariano

Thanks for your answer

Yes, I am using the code explained in that link. That is the code that causes the error.

I made some changes and now at the end I get an error explaining that the app was designed some time ago and does not comply with the new Android security rules.

Any other recommendations?
 
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
Hello Mariano

Thanks for your answer

Yes, I am using the code explained in that link. That is the code that causes the error.

I made some changes and now at the end I get an error explaining that the app was designed some time ago and does not comply with the new Android security rules.

Any other recommendations?
I understand. Have you tried testing it in B4J?
Edit: Try that example in B4J, depending on what you are trying to do I think it will be exported to B4A
 

Attachments

  • jsoup_demo modified.zip
    5.6 KB · Views: 22
Last edited:
Upvote 0

DanteS

Member
Licensed User
Longtime User
Thanks again Mariano

I am trying your version

When I am compiling it, it generates this error

B4A Version: 12.80
Parsing code. Error
Error parsing the program.
Error Description: Unknown Type: b4xmainpage
Are you missing a library reference?
An error has occurred on line: 73 (B4XPages)
Public submain page as B4XMainPage


But there is not 73 line. Code ends on line 68

68 'Program code should go into B4XMainPage and other pages.

I don´t see B4XMainPage and other pages.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
Thanks again Mariano

I am trying your version

When I am compiling it, it generates this error

B4A Version: 12.80
Parsing code. Error
Error parsing the program.
Error Description: Unknown Type: b4xmainpage
Are you missing a library reference?
An error has occurred on line: 73 (B4XPages)
Public submain page as B4XMainPage


But there is not 73 line. Code ends on line 68

68 'Program code should go into B4XMainPage and other pages.

I don´t see B4XMainPage and other pages.
Extract the everything as it is a B4XPages project.

DONT EXTRACT ONLY THE B4A FOLDER
 
Upvote 0

DanteS

Member
Licensed User
Longtime User
Thanks a lot Mariano

I had not loaded the .bas file
Finally I could run the app.
It is not what I needed but I learned a lot working with this code.
Once again, thank you very much
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
It is not what I needed but I learned a lot working with this code.
What do you need, another example in B4A or other options in this 9 year old jSoup library?
 
Upvote 0

DanteS

Member
Licensed User
Longtime User
I'm not sure which of these two situations I want. I'm still trying to find some code with a method_1, that sends a URL to the web to call a page, and a function_1 that can read the response data.

In the graphical environment, two EditText and two buttons. One could enter the URL in EditText_1 and invoke method_1 to submit the URL by clicking button_1. Then function_1 fetches the response and writes it to EditText_2
Once the data is in EditText_2, any technique can be applied to capture the price of each car including the response, by clicking the button_2
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
@DanteS: So what you describe above is web scraping. Because there are many and all different websites, there is no standard one-fit-all solution. jSoup can do a lot, but it must be tailored to the website you want to use.

I understand that you want to get the prices off a website, but you don't tell us which website you want to use for your web scraping.

Can you give the URL of the website whose prices you want to use?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User

Scrapping Jsoup​

I recommend that you first develop your solution in B4J as a B4XPages a B4X solution. Development, and especially scraping, is easier to develop a B4A solution with a keyboard than on a bigger screen. If it works in B4J, you only need minimal adjustments when using it in B4A.

Because the jSoup library is old and not up to date and, as always with a library, does not contain the routine you need or want.

The following is needed:

1 Download the latest jSoup jar:
Download the latest jSoup jar from here: https://jsoup.org/download and save it in the external libray folder

2 Add on the Main page:
#AdditionalJar: jsoup-1.17.2 which is the same as the name of the current version of jSoup

3 Create separate B4XPages with only jSoup routines:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
   
    Private clv1 As CustomListView
    Private txturl As B4XFloatTextField

End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("frmDantesjSoup")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub txturl_EnterPressed
'    Me.As(JavaObject).RunMethod("firstScrape", Array As String("https://quotes.toscrape.com/"))
    Me.As(JavaObject).RunMethod("firstScrape", Array ("https://b4x.com"))
   
End Sub

#IF JAVA
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

    /**
     * --- The firstScrape is an jSoup example to start with jsoup.
     *     1) Download the latest jSoup jar from here: https://jsoup.org/download
     *     2) Add in Main page: #AdditionalJar: jsoup-1.17.2
     *     3) Call this routine with:
     *        Me.As(JavaObject).RunMethod("firstScrape", Array ("https://b4x.com"))
     *    
     *     Happy scraping!
     *  
     *     One of many information on the Internet to start:
     *     https://www.tutorialspoint.com/jsoup/index.htm
     * ---
     * @param url the web page to scrape
     * @throws IOException
     */

    public void firstScrape(String url) throws IOException {
        // Connect to the target website with an HTTP GET request
        Document doc = Jsoup.connect(url).userAgent(
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
                .get();

        // Extract the title of the webpage
        String title = doc.title();
        System.out.println(title);

        // Extract the textual part of the web page
        String text = doc.body().text();
        System.out.println(text);
       
        System.out.println("----");

        // Extract all hyperlinks in the URL
        Elements links = doc.select("a[href]");
        for (Element link : links) {
            String linkHref = link.attr("href");
            String linkText = link.text();
            String linkOuterHtml = link.outerHtml();
            String linkInnerHtml = link.html();
            System.out.println(linkHref + "\t" + linkText + "\t" + linkOuterHtml + "\t" + linkInnerHtml);
        }
    }
#End If

Private Sub clv1_ItemClick (Index As Int, Value As Object)
   
End Sub

As you can see, I use the modern B4X views to save space. By applying the hint text and usage masking from the Enterpress routine, both the button and a label become redundant. This saves space on a small screen.

Finally:​

Do not hesitate to ask a question on this forum, but please add a small example project in which you demonstrate:
  1. What you want to achieve
  2. Enter the URL in the program
  3. Where the problem is
  4. What's missing.
Update: external library folder as destination added
 
Last edited:
Upvote 0

DanteS

Member
Licensed User
Longtime User
I recommend that you first develop your solution in B4J as a B4XPages a B4X solution. Development, and especially scraping, is easier to develop a B4A solution with a keyboard than on a bigger screen. If it works in B4J, you only need minimal adjustments when using it in B4A.
I stopped programming for Android in 2015 and I am returning just now, so I lost all the changes of the B4A platform, when B4J, B4i, B4X, etc... where included. I also have not installed the corrsponding developping environ, but after two weeks trying to beging to run again, I have understood that I need to walk that road. Just give me a couple of weeks.
I understand that you want to get the prices off a website, but you don't tell us which website you want to use for your web scraping.

Can you give the URL of the website whose prices you want to use?
The URL is this:
https://finance.yahoo.com/quote/MA, where the MA, at the end, is the ticket code of the stock (in this case Master Card). When the answer returns, the page shows a lot of information about the performance of the stock, including the instant price, the amount of today change, etc. and one need to scrape the text to find and read the values you need. Fortunately when you send https://finance.yahoo.com/quote/MCD (for MacDonalds), the values are located at the same places.
 
Upvote 0
Top