I am trying to parse HTML files from my local file system using Jsoup. I have created a small Android application, where the user can input filename and gets the title of the HTML file displayed in TextView. However, each time I try to get the title from the Document element, I get null. How can I fix this error? Here is my code for the button click function:
public void onClick(View view){
MyTask myTask = new MyTask();
myTask.execute();
}
lass MyTask extends AsyncTask<Void,Void,Void>{
String title;
@Override
protected Void doInBackground(Void... voids) {
Document doc = null;
File file = new File("file:///android_asset/olx.html");
try {
doc = Jsoup.parse(file, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
if (doc != null)
title = "success" + doc.title();
else
title = "Error. Can't read title! ";
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
outputText.setText(title);
}
}
请使用这个。
String title = doc.select("head title").text();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.