我正在为大学项目做一个Android应用程序。我想阅读使用jsoup的两个网站的文本。我在使用jsoup从URL创建文档时遇到错误:
"错误:(50,227)错误:不兼容的类型:org.jsoup.nodes.Document无法转换为org.w3c.dom.Document"
已经看过这么多教程,但对其他任何人来说似乎都不是问题。非常感谢帮助弄清楚它为什么不起作用!
这是我的代码:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import java.io.FileWriter;
import java.io.PrintWriter;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new getData().execute();
}
});
}
public class getData extends AsyncTask<Void, Void, Void>{
String avgT;
String avgRain;
String fileNameR = "Rainfile.txt";
String fileNameT = "Tempfile.txt";
@Override
protected Void doInBackground(Void... params) {
Document temp;
Document rain;
try {
temp = Jsoup.connect("http://www.timeanddate.com/weather/singapore/singapore/historic").get(); //avg temp
avgT=temp.getTextContent();
FileWriter fw1 = new FileWriter(fileNameT);
PrintWriter pw1 = new PrintWriter(fw1);
pw1.println(avgT);
pw1.close();
rain = Jsoup.connect("http://www.weatheronline.co.uk/weather/maps/city?LANG=en&WMO=48698&ART=PRE&CONT=asie&R=0&LEVEL=150®ION=0027&LAND=SX&NOREGION=0&MOD=&TMX=&TMN=&SON=&PRE=&MONAT=&OFFS=&SORT=").get();
avgRain=rain.getTextContent();
FileWriter fw2 = new FileWriter(fileNameR);
PrintWriter pw2 = new PrintWriter(fw2);
pw2.println(avgRain);
pw2.close();
}catch (Exception e){e.printStackTrace();} //http://www.timeanddate.com/weather/singapore/singapore/historic
return null;
}
}
}
`
答案 0 :(得分:1)
您导入的文档类型错误。改变这一行
import org.w3c.dom.Document;
到
import org.jsoup.nodes.Document;