【问题标题】:How to return a String Array using Jsoup?如何使用 Jsoup 返回字符串数组?
【发布时间】:2013-01-10 07:26:31
【问题描述】:

如何将使用 jsoup 库提取的所有 href 链接存储到字符串数组?

然后将其全部显示在 TextView 中?

我不知道如何将 AsyncTaskString Array 一起使用,也不知道如何在提取过程中执行 FOR LOOP来自 Google 的 href 链接。我不知道该设置什么条件才能使 FOR LOOP 停止。我当前的代码只返回最后一个 href 链接。我希望有人可以向我说明。感谢您的宝贵时间!

package com.example.jsouptestarray;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.example.jsouptestarray.R;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new MyTask().execute();

         }


    private class MyTask extends AsyncTask<Void, Void, String> {

          @Override
          protected String doInBackground(Void... params) {

            Document doc;
            String linkText = ""; 

            try {
                doc = Jsoup.connect("https://www.google.com/").get();
                 Elements links = doc.getElementsByTag("a");
                 for (Element el : links) { 
                     linkText = el.attr("href");
                        System.out.println("Href Found!");
                        System.out.println("Href attribute is : "+linkText);
                 }


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


            return linkText;   
          } 


          @Override
          protected void onPostExecute(String result) {        
            //if you had a ui element, you could display the title
            ((TextView)findViewById (R.id.textView2)).append ( result  );
          }
        }

}

【问题讨论】:

    标签: android json parsing android-asynctask jsoup


    【解决方案1】:

    将您的 AsyncTask 类更改为从 doInBackground 返回 String ArrayList :

    private class MyTask extends AsyncTask<Void, Void, ArrayList<String>> {
    
    ArrayList<String> arr_linkText=new ArrayList<String>();
    
              @Override
              protected ArrayList<String> doInBackground(Void... params) {
    
                Document doc;
                String linkText = ""; 
    
                try {
                    doc = Jsoup.connect("https://www.google.com/").get();
                     Elements links = doc.getElementsByTag("a");
                     for (Element el : links) { 
                         linkText = el.attr("href");
                         arr_linkText.add(linkText); // add value to ArrayList
                     }
                  } catch (IOException e) {
                    // TODO Auto-generated catch block
                   e.printStackTrace();
                 }
                return arr_linkText;     //<< retrun ArrayList from here
              } 
    
    
              @Override
              protected void onPostExecute(ArrayList<String> result) {        
    
              // get all value from result to display in TextView
                   TextView textview=(TextView)findViewById(R.id.textView2);
                  for (String temp_result : result) {
                    System.out.println("links :: "+temp_result);
    
                               textview.append (temp_result +"\n");
                 }
              }
            }
    

    【讨论】:

    • 在"return arr_linkText;"处它告诉我最终插入以完成 TryStatment .. 如何修复它?同样在执行 For 循环时的“onPostExecute”上,如何找出数组中有多少个 href 链接?你能更新一下你的答案吗?谢谢!
    • 它在系统日志上精美地打印出来,只是一件小事。你怎么能在 TextView 中用新的一行打印出来呢?谢谢!
    • 没关系,我让它工作了。您是救生员,非常感谢您的帮助!
    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多