Android 如何使用Jsoup返回字符串数组?
如何将使用jsoup库提取的所有href链接存储到字符串数组中 然后将其全部显示在文本视图中 我不知道如何将AsyncTask与字符串数组一起使用,也不知道如何在从Google提取href链接的过程中执行FOR循环。我不知道如何设置条件以使for循环停止。我的当前代码只返回最后一个href链接。我希望有人能给我举例说明。谢谢你的时间Android 如何使用Jsoup返回字符串数组?,android,json,parsing,android-asynctask,jsoup,Android,Json,Parsing,Android Asynctask,Jsoup,如何将使用jsoup库提取的所有href链接存储到字符串数组中 然后将其全部显示在文本视图中 我不知道如何将AsyncTask与字符串数组一起使用,也不知道如何在从Google提取href链接的过程中执行FOR循环。我不知道如何设置条件以使for循环停止。我的当前代码只返回最后一个href链接。我希望有人能给我举例说明。谢谢你的时间 package com.example.jsouptestarray; import java.io.IOException; import org.jsoup
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 );
}
}
}
package com.example.jsouptestarray;
导入java.io.IOException;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入org.jsoup.nodes.Element;
导入org.jsoup.select.Elements;
导入com.example.jsouptestarray.R;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.app.Activity;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
文本查看文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建MyTask().execute();
}
私有类MyTask扩展了AsyncTask{
@凌驾
受保护字符串doInBackground(无效…参数){
文件文件;
字符串linkText=“”;
试一试{
doc=Jsoup.connect(“https://www.google.com/).get();
元素链接=doc.getElementsByTag(“a”);
对于(元素el:links){
linkText=el.attr(“href”);
System.out.println(“Href Found!”);
System.out.println(“Href属性为:+linkText”);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回链接文本;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//如果有ui元素,则可以显示标题
((TextView)findViewById(R.id.textView2)).append(result);
}
}
}
将AsyncTask
类更改为从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");
}
}
}
私有类MyTask扩展了AsyncTask{
ArrayList arr_linkText=新的ArrayList();
@凌驾
受保护的ArrayList doInBackground(无效…参数){
文件文件;
字符串linkText=“”;
试一试{
doc=Jsoup.connect(“https://www.google.com/).get();
元素链接=doc.getElementsByTag(“a”);
对于(元素el:links){
linkText=el.attr(“href”);
arr_linkText.add(linkText);//向ArrayList添加值
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
return arr_linkText;//在“return arr_linkText”处,它告诉我最后插入以完成约会..如何修复它?也在“onPostExecute”上当我进行For循环时,我如何知道数组中有多少个href链接?请在回答时更新它?谢谢!它在系统日志上打印得很漂亮,只是一件小事。你怎么能在文本视图中用新行打印出来?谢谢!不用管我让它工作了。你是个救命恩人,我感谢你真是帮了大忙!