我使用下面的代码将一些网页的内容保存在一个方法中,但是我一直存在这样的错误:(“无法从ConvertUrlToString类型中静态引用非静态方法转换器()”)
请考虑我刚才使用这个类来查找我的主要项目的问题,在另一个类中有类似的方法(转换器()),而没有“公共静态空主(String args[])”。
package testing;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class ConvertUrlToString {
public static void main(String args[]) throws IOException{
convertor();
}
public void convertor() throws IOException
{
Document doc = Jsoup.connect("http://www.willhaben.at/iad/immobilien/mietwohnungen/wien/wien-1060- mariahilf/").get();
String text = doc.body().text();
Document doc1 = Jsoup.connect("http://www.google.at/").get();
String text1 = doc1.body().text();
Document doc2 = Jsoup.connect("http://www.yahoo.com/").get();
String text2 = doc2.body().text();
System.out.println(text);
System.out.println(text1);
System.out.println(text2);
}
}发布于 2015-01-14 21:13:12
只需在yout转换器方法中添加一个静态关键字即可。由于您试图在静态上下文(main方法)中调用它,所以该方法必须是静态的。如果不想使其成为静态的,则需要创建一个ConvertUrlToString实例,并由它们调用转换器方法。
public static void main( String[] args ) {
try {
convertor();
catch ( IOException exc ) {
}
}
public static void convertor() throws IOException {
...
}或
public static void main( String[] args ) {
try {
new ConvertUrlToString().convertor();
catch ( IOException exc ) {
}
}
public void convertor() throws IOException {
...
}https://stackoverflow.com/questions/27952524
复制相似问题