简体   繁体   English

Java返回类型与WebCrawler.visit(Page)不兼容

[英]Java The return type is incompatible with WebCrawler.visit(Page)

I'm using some crawler code from http://code.google.com/p/crawler4j/ . 我正在使用http://code.google.com/p/crawler4j/中的某些搜寻器代码。

Now, what I'm trying to do is to access every URLs found in the MyCrawler class from another class. 现在,我想做的是从另一个类访问MyCrawler类中找到的每个URL。

I start the crawler with : 我通过以下方式启动搜寻器:

// * Start the crawl. This is a blocking operation, meaning that your code
// * will reach the line after this only when crawling is finished.
controller.start(MyCrawler.class, numberOfCrawlers); 

When I try to use "return" to get my URLs, I get this error : 当我尝试使用“返回”获取我的网址时,出现以下错误:

The return type is incompatible with WebCrawler.visit(Page)

and it asks me to change the type to 'void' but, of course, I don't want to. 它要求我将类型更改为“ void”,但我当然不愿意。

Here's the function that I have trouble with : 这是我遇到的功能:

@Override
public  String visit(Page page) {          
        url = page.getWebURL().getURL();
        System.out.println("URL: " + url);

        if (page.getParseData() instanceof HtmlParseData) {
                HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
                String text = htmlParseData.getText();
                String html = htmlParseData.getHtml();
                List<WebURL> links = htmlParseData.getOutgoingUrls();

                System.out.println("Text length: " + text.length());
                System.out.println("Html length: " + html.length());
                System.out.println("Number of outgoing links: " + links.size());

              return url;  

        }

I also tried to use a getter but since it is a "blocking operation", it doesn't work. 我也尝试使用吸气剂,但是由于它是“阻塞操作”,因此不起作用。 I'm running out of ideas. 我的想法不多了。

You can't override a method if you change the method signature. 如果更改方法签名,则无法覆盖方法。 If you change the signature you are making a new method. 如果更改签名,那么您将采用一种新方法。 If all you want is the list of urls you visited, instead of returning the urls, try storing them in an ArrayList and make a getter which returns the list. 如果您只需要访问的URL列表,而不是返回URL,请尝试将它们存储在ArrayList中,并创建一个可返回列表的getter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM