简体   繁体   English

无法在Android应用程序中读取html文件标题Jsoup

[英]Can't read html file title Jsoup in Android application

I am trying to parse HTML files from my local file system using Jsoup. 我正在尝试使用Jsoup解析本地文件系统中的HTML文件。 I have created a small Android application, where the user can input filename and gets the title of the HTML file displayed in TextView. 我创建了一个小的Android应用程序,用户可以在其中输入文件名并获取TextView中显示的HTML文件的标题。 However, each time I try to get the title from the Document element, I get null. 但是,每次我尝试从Document元素获取标题时,都会得到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();

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

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