如何使用Jsoup返回String数组?

时间:2013-01-10 07:26:31

标签: android json parsing android-asynctask jsoup

如何将使用jsoup库提取的所有href链接存储到String Array?

然后在TextView中显示所有内容?

我不知道如何在字符串数组中使用 AsyncTask ,在提取过程中我也不知道如何执行 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  );
          }
        }

}

1 个答案:

答案 0 :(得分:2)

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");
             }
          }
        }