【问题标题】:Taking an webpage element with Jsoup in AsyncTask in a fragment (Android Newbie)在片段中的 AsyncTask 中使用 Jsoup 获取网页元素(Android 新手)
【发布时间】:2019-03-30 11:44:38
【问题描述】:

我是 Java 和 Android 的新手,所以我需要您的帮助。 我在我的应用程序中实现了 JSoup 以从网页中获取它并在文本视图中显示(我在片段中操作,但我认为在这种情况下它与标准活动相同)。

<body marginwidth="0" marginheight="0">
<h1></h1>
<p class="testoprezzo">0.5516 </p>
</body>

我只需要 0.5516

我不知道该怎么做。你能帮助我吗? 这是我已经编写的代码:
class fetcher extends AsyncTask<Void,Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        try {

            String value = "https://mywebpage.net/";
            Document document = Jsoup.connect(value).followRedirects(false).timeout(30000).get();
            Element p= document.select ("p.testoprezzo").first();
            ((Globals)getApplication()).setValore(p.text());




        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);


        TextView valore = findViewById(R.id.textView4);
        valore.setText(((Globals)getApplication()).getValore());








    }
}

提前感谢您!

【问题讨论】:

    标签: java android android-fragments android-asynctask jsoup


    【解决方案1】:

    尽量使用最严格的选择器,以免得到不相关的结果。在您的情况下,您应该使用 -

    Element e = doc.select("p.testoprezzo").first();
    String result = p.text();
    

    【讨论】:

      【解决方案2】:

      使用Elements获取p标签。

      class fetcher extends AsyncTask<Void,Void, Void> {
      String txtValue;
      
      @Override
      protected Void doInBackground(Void... arg0) {
      
          try {
      
              String value = "https://mywebpage.net/";
              Document document = Jsoup.connect(value).followRedirects(false).timeout(30000).get();
              Element p= document.select ("p.testoprezzo").first();
              txtValue = p.text();
      
      
      
      
          } catch (Exception e) {
              // TODO Auto-generated catch block
              txtValue = null;
              e.printStackTrace();
          }
      
          return null;
      }
      
      @Override
      protected void onPostExecute(Void aVoid) {
          super.onPostExecute(aVoid);
      
      
          TextView valore = findViewById(R.id.textView4);
          valore.setText(txtValue);
      }
      

      }

      请注意 ElementsElement 是不同的。根据您的需要使用它们。 这是所有selectors 的列表以及示例。

      另请注意:不要做任何 U.I.更改doInBackground方法中的,否则会报错。

      【讨论】:

      • 我试过了,但是 Android Studio 没有解析 Element p= document.select("p").first(); 中的方法 ''select''
      • 确保您在导入部分导入类似 import org.jsoup.nodes.Document; 的内容,而不是其他内容。根据您的项目,有 3 -4 种类型的文档可用。
      • 最后一个问题,如何将结果字符串的值带到文本视图中?我尝试过 onPostExecute 但似乎在片段中的工作方式不同
      • 声明一个全局字符串变量并在doInBackground 上将p 的值分配给您的字符串变量,然后在onPostExecute 方法中写入这一行yourTextView.setText(your_string_variable_name);
      • 嗯...由于某些原因它不起作用,但我不知道为什么,应用程序运行,但文本不出来
      猜你喜欢
      • 2016-10-06
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多