带有AsyncTask的Android JSoup-无法查看内容
我使用此代码是为了获取某些网站的内容。 文本视图保持为空。我做错了什么? 我将jar添加到librires中,并将互联网权限添加到清单中带有AsyncTask的Android JSoup-无法查看内容,android,Android,我使用此代码是为了获取某些网站的内容。 文本视图保持为空。我做错了什么? 我将jar添加到librires中,并将互联网权限添加到清单中 public class MainActivity extends Activity { MyTask mt; TextView tvInfo; String URL="http://www.example.com/"; public void onCreate(Bundle savedInstanceState) { super.onCr
public class MainActivity extends Activity {
MyTask mt;
TextView tvInfo;
String URL="http://www.example.com/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvInfo = (TextView) findViewById(R.id.textView1);
}
public void onclick(View v) {
mt = new MyTask();
mt.execute(URL);
}
class MyTask extends AsyncTask<String, Void, String> {
Document doc;
String title=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
tvInfo.setText("Please wait");
}
@Override
protected String doInBackground(String... params) {
try {
TimeUnit.SECONDS.sleep(2);
// doc = Jsoup.connect(params[0]).get();
// String title = doc.title();
doc = Jsoup.connect("http://www.example.com/").get();
Element content = doc.select("a").first();
title = content.text();
Log.d("AsyncTask doInBackground","URL: " + params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvInfo.setText(title);
}
}
}
我也不明白这里的每个方法何时被调用
非常感谢
编辑-答案建议后的代码。仍然不起作用:
public class MainActivity extends Activity implements OnClickListener{
MyTask mt;
TextView tvInfo;
String URL="http://www.example.com/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvInfo = (TextView) findViewById(R.id.textView1);
tvInfo.setOnClickListener(this);
}
private class MyTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String title = "hh";
try{
Document doc = Jsoup.connect("http://google.com").userAgent("Mozilla").get();
title = doc.title();
System.out.println("title : " + title);
// get all links
Elements links = doc.select("a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
}
catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
tvInfo.setText(result);
}
@Override
protected void onPreExecute() {
tvInfo.setText("Please wait");
}
}
@Override
public void onClick(View v) {
mt = new MyTask();
mt.execute(URL);
}
}
AsyncTask在另一个线程内执行doInBackground中的所有内容,该线程无权访问视图所在的GUI preExecute和postExecute为您提供了在这个新线程中发生繁重操作之前和之后对GUI的访问,您甚至可以将长操作的结果传递给postExecute,然后显示任何处理结果
private class MyTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try{
Document doc = Jsoup.connect("http://google.com").userAgent("Mozilla").get();
String title = doc.title();
System.out.println("title : " + title);
// get all links
Elements links = doc.select("a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
tvInfo.setText("Please wait");
}
}
谢谢您的演示。@user1932595 No代替您的myTask类。让主要活动保持原样,如果你觉得我的答案有用,请投票表决并接受它作为答案。感谢you@user1932595注意:您的代码是错误的。我已经编辑了您的问题,以便很好地执行onClickListner。使用新代码,它应该打印在领事馆output@user1932595添加我之前放置的新行,请稍候,然后重试…super.onPreExecute;