2013-01-10 46 views
0

如何將使用jsoup庫提取的所有href鏈接存儲到字符串數組?如何使用Jsoup返回一個字符串數組?

然後將它全部顯示在TextView中?

我不知道如何使用的AsyncTask字符串數組,我也不知道如何提取從谷歌的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 ); 
      } 
     } 

} 

回答

2

更改AsyncTask類爲從doInBackground返回字符串的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"); 
      } 
      } 
     } 
+0

在 「迴歸arr_linkText;」它告訴我最後插入來完成TryStatment ..如何解決它?當我執行For循環時,還要在「onPostExecute」上找到在數組中有多少個href鏈接?你可以請你的答案更新嗎?謝謝! – Questions

+0

@問題:請參閱我的編輯答案 –

+0

它在系統日誌中打印出精美,只是一件小事。你怎麼也可以用一個新的行在TextView中打印出來?謝謝! – Questions

相關問題