Android JSoup with AsyncTask - 看不到内容

时间:2013-08-20 12:04:20

标签: android

我使用此代码来获取某些网站的内容。 textview保持空白。我做错了什么? 我将jar添加到librires中,并添加了对manifest的Internet权限。

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);
    }

}

1 个答案:

答案 0 :(得分:0)

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");
      }

}