2

JSOUP を使用して Web ページ (この場合は google.com) からデータを取得しようとしていて、デバッグ時にタイトル データが返されて logcat に表示されるという問題がありますが、テキストビューが更新されないようです。取得したてのデータで。

ソース:

package com.example.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;



import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.TextView01);
    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                     Document doc = Jsoup.connect("http://google.com")
                                .userAgent("Mozilla")
                                .get();
                    // get page title
                    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();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String title) {
            textView.setText(title);
        }
    }

    public void onClick(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.google.com" });
    }
}

編集:(スーパーユーザーの提案に応じて-ハンドラーの実装)

public class MainActivity extends Activity {
    private TextView textView;
     private Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.TextView01);
        handler = new Handler();
    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                     Document doc = Jsoup.connect("http://google.com")
                                .userAgent("Mozilla")
                                .get();
                    // get page title
                    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();

                }
                    handler.post(new Runnable() {
                    @Override
                    public void run() {}});

            }
            return response;
        }

        @Override
        protected void onPostExecute(String title) {
            textView.setText(title);
            View.invalidate();
        }
    }

    public void onClick(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.google.com" });

    }
}

結果(上記の編集から):

Cannot make a static reference to the non-static method invalidate() from the type View MainActivity.java   
Cannot refer to a non-final variable title inside an inner class defined in a different method  MainActivity.java
4

2 に答える 2

2

申し訳ありませんが、昨日この質問に答えようとしましたが、キーボードで眠りに落ちました :P

しかし、結果の string:protected void onPostExecute(String result)には何も渡されません。問題は簡単に解決されます。

  1. あなたのonCreateの上:

String title;

  1. doInBackGround で:

title = doc.title();

  1. onPostExecute で:

    @Override
    protected void onPostExecute(String result) {
        textView.setText(title);
    }
    
于 2013-09-11T19:26:32.233 に答える