jsoup ライブラリを使用して抽出したすべての href リンクを文字列配列に保存するにはどうすればよいですか?
次に、TextView 内にすべて表示しますか?
String ArrayでAsyncTaskを使用する方法も、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 );
}
}
}