Jsoup.connect()使用Java,不与Android [英] Jsoup.connect() working with Java, not with Android

查看:217
本文介绍了Jsoup.connect()使用Java,不与Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过Jsoup网站上给出的 Jsoup.connect()的例子,它在Java中正常工作。

由于某些原因,我不能让它在Android的项目(Eclipse中)工作,即使我允许我的 AndroidManifest 互联网访问权限。该Jsoup库安装正确,我可以用 Jsoup.parse()工作,没有任何问题。下面是$ C在Java的什么工作$ CS和几行也允许 AndroidManifest

的Java

 公共静态无效的主要(字串[] args){
    文档文档;
    尝试{
        DOC = Jsoup.connect(http://google.ca/)获得();
        字符串title = doc.title();
        System.out.print(职称);
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }

的Andr​​oidManifest.xml

 <采用-SDK安卓的minSdkVersion =12/>
<使用许可权的android:NAME =android.permission.INTERNET对/>
<应用

当我尝试运行它,它崩溃和日志说:

  20 01-09:19:30.560:E / AndroidRuntime(26839):了java.lang.RuntimeException:
无法启动活动
ComponentInfo {} com.mrdroidinator.com/com.mrdroidinator.com.Parselhjmq:android.os.NetworkOnMainThreadException


解决方案

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

问题是,你的主线程,这是在API层面11+禁止进行网络操作。这是因为如果你这样做的用户界面被冻结,直到文件完成下载,所以它是需要在不同的线程,从而不影响性能比较UI进行这样的操作。

这是你如何开始一个新的线程:

 发downloadThread =新主题(){
  公共无效的run(){
    文档文档;
    尝试{
      DOC = Jsoup.connect(http://google.ca/)获得();
      字符串title = doc.title();
          System.out.print(职称);
    }赶上(IOException异常五){
          e.printStackTrace();
    }
  }
};
downloadThread.start();

I've tried the Jsoup.connect() example given on the Jsoup website and it works fine in Java.

For some reason, I can't make it work in Android Projects (Eclipse) even though I allow the Internet access permission in my AndroidManifest. The Jsoup library is installed correctly and I can work with Jsoup.parse() without any issues. Here's a few line of codes of what works in Java and also the permission in AndroidManifest.

Java

public static void main(String[] args){
    Document doc;
    try {
        doc = Jsoup.connect("http://google.ca/").get();
        String title = doc.title();
        System.out.print(title);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

AndroidManifest.xml

<uses-sdk android:minSdkVersion="12" />
<uses-permission android:name="android.permission.INTERNET"/>
<application

When I try to run it, it crashes and the log says:

01-09 20:19:30.560: E/AndroidRuntime(26839): java.lang.RuntimeException: 
Unable to start activity 
ComponentInfo{com.mrdroidinator.com/com.mrdroidinator.com.Parselhjmq}: android.os.NetworkOnMainThreadException

解决方案

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

The problem is that you are performing a network operation on the main thread, which is prohibited in API level 11+. This is because if you do it the UI is "frozen" until the document finishes downloading, so it is needed to perform such operations on a different thread, which doesn't affect UI perfomance.

This is how you start a new thread:

Thread downloadThread = new Thread() {
  public void run() {
    Document doc;
    try {
      doc = Jsoup.connect("http://google.ca/").get();
      String title = doc.title();
          System.out.print(title);
    } catch (IOException e) {
          e.printStackTrace();
    }
  }
};
downloadThread.start();

这篇关于Jsoup.connect()使用Java,不与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆