【问题标题】:android HTML Scraping with Jsoup crashesandroid HTML Scraping with Jsoup 崩溃
【发布时间】:2014-07-03 16:07:21
【问题描述】:

我尝试编写一个简单的应用程序,使用 Jsoup 将 TextView 更改为 Google Html 上的标题。但是当我运行它时,应用程序崩溃了。有人知道为什么吗? 我认为 Asynk 任务可能有问题,有人可以看看代码并告诉我错误是什么吗?

import java.io.IOException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


public class HttpExample extends Activity{

TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.http);
    tvResult = (TextView)findViewById(R.id.tvHttp);

     new Title().execute();
}

 private class Title extends AsyncTask <Void,Void,Void>{
     String title;
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            // Connect to the web site
            Document document = Jsoup.connect("http://www.google.com").get();
            // Get the html document title
            title = document.title();
        } catch (IOException e) {

                e.printStackTrace();
        }
        return null;
    }//doInBackground()

    protected void onPostExecute(Void result) {
        // Set title into TextView
       tvResult.setText(title);

    }//onPostExecute()


    }//Title class


 }//Activity

非常感谢您的回答...

【问题讨论】:

  • 我试过了,没有崩溃。您是否添加了访问 Internet 的权限?抛出什么样的异常?

标签: java android json android-asynctask web-scraping


【解决方案1】:

在android清单文件中添加以下权限,应用程序不会崩溃。

<uses-permission android:name="android.permission.INTERNET" />

【讨论】:

  • 你不可能知道
  • 他没有提到任何关于添加权限的事情。所以我们可以断定他没有:)
  • OP也没有提到布局,所以同样我们可以断定文本视图不在布局中。
  • 也许你是对的,或者我是对的。让 OP 添加更多代码部分或线索以找出问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 2015-05-27
  • 2020-10-14
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多