java - 无法使用 Android/JSOUP 解析 HTML 数据

标签 java android android-asynctask jsoup httpresponse

我遇到一个问题,我尝试使用 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" });
    }
}

编辑:(响应 super 用户的建议 - 实现处理程序)

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

最佳答案

抱歉,昨天正准备回答这个问题,但是在键盘上睡着了:P

但是您的结果字符串:protected void onPostExecute(String result) 没有传递任何内容。问题就很容易解决了。

  1. 在 onCreate 上方:

字符串标题;

  1. 在你的 doInBackGround 中:

title = doc.title();

  1. 在 onPostExecute 中:

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

关于java - 无法使用 Android/JSOUP 解析 HTML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18728984/

相关文章:

java - 由 : java. lang.IllegalArgumentException 引起:主机名不能为空

java - 在 Swing 中不同类的 JPanel 上绘画

java - 使用 FileUpload 时嵌套表单上的 Wicket 口问题

java - android.util.AndroidRuntimeException : Calling startActivity() from outside of an Activity

android - android 和 vb.net 之间通过套接字进行通信

android - 如何取消运行 BitmapFactory.decodeFile() 的 AsyncTask 并进行清理

java - 如何修改 jbpm-console 应用程序以使其从数据库检索用户、角色和组列表?

java - 在java中读取文件时,字符之间存在空格

java - 使用温和的传输

java - 如何检查android设备是否可以连接网络?