0

このコードを使用して、Web サイトのコンテンツを取得します。テキストビューは空のままです。私が間違っていることは何ですか?jar をライブラリに追加し、マニフェストにインターネット アクセス許可も追加しました。

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

}
4

1 に答える 1

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

}   
于 2013-08-20T12:21:00.953 に答える