Two jsoup scrapers in one Android app doesn't work

Viewed 52

I'm developing a simple Android application that has to scrape content of one website and then add this to the URL of another website that I have to scrape. The first scraping is okay, but the second gives me an error: "HTTP error fetching URL". The second URL is fine because when I give it to the first scraper everything is okay (but the URL is changing, so I cannot use it as a constraint). I think that I have to finish the thread of the first scraper somehow, but I don't know how. My current code:

private void getBodyText() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final StringBuilder builder = new StringBuilder();
                try {
                    String url="http://example.com";
                    Document doc = Jsoup.connect(url).get();
                    Element body = doc.body();
                    builder.append(body.text());
                } catch (Exception e) {
                    builder.append("Error : ").append(e.getMessage()).append("\n");
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(builder.toString());
                        token1=builder.toString();
                        checkPairing();
                    }
                });
            }
        }).start();
    }
    private void checkPairing() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final StringBuilder builder = new StringBuilder();
                try {
                    String url="http:example.com/"+token1;
                    Document doc = Jsoup.connect(url).get();
                    Element body = doc.body();
                    builder.append(body.text());
                } catch (Exception e) {
                    builder.append("Error : ").append(e.getMessage()).append("\n");
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (builder.toString()!="Not Found") {
                            tokenPar = builder.toString();
                        }
                    }
                });
            }
        }).start();
    }

I've also tried to execute the second function directly from onCreate() after the first scraper is executed but nothing changed.

0 Answers
Related