2

我遇到了一个问题,我试图使用 JSOUP 从网页(在本例中 - google.com)获取数据,并且在调试时返回并显示在 logcat 中的标题数据 - 但是我的 textview 似乎从未更新与新获得的数据。

资源:

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

但是你的结果字符串: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 回答
0

尝试将 textview 作为构造函数参数传递给您的类 DownloadWebPageTask。

DownloadWebPageTask task = new DownloadWebPageTask(textView);

在您的 DownloadWebPageTask 类中定义一个 TextView 变量来保存该对象。

在 onPostExecute() 方法中更新相同的内容。

于 2013-09-11T02:04:27.070 回答