How can I store all the href links I extracted using jsoup library to a String Array?
Then display it all inside a TextView?
I do not know how to use AsyncTask with String Array, nor do I know how to do a FOR LOOP during the extractions the href links from Google. I don't know what to put for the condition to make the FOR LOOP stop. My current code only returns the last href link. I hope someone can illustrate it to me. I appreciate your time!
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 );
}
}
}