0

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

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

我不知道如何将AsyncTaskString Array一起使用,也不知道如何在从 Google 提取 href 链接期间执行FOR LOOP 。我不知道该设置什么条件才能使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  );
          }
        }

}
4

1 回答 1

2

将您的课程更改AsyncTask为从以下位置返回 String ArrayList doInBackground

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");
             }
          }
        }
于 2013-01-10T07:35:24.360 回答