与jsoup解析抛出错误(NetworkOnMainThreadException) [英] Parsing with jsoup throws error (NetworkOnMainThreadException)

查看:552
本文介绍了与jsoup解析抛出错误(NetworkOnMainThreadException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了jsoup库 jsoup-1.7.1.jar核心并使用项目它导入到我的项目 - >属性 - > Java构建路径 - >添加外部JAR 我贴库文件到我的libs文件夹。不过似乎有关于导入Jsoup库我的项目的一些问题。当我运行我的应用程序,启动后,我得到这个错误。

  22 12-26:59:24.133:E / AndroidRuntime(6710):致命异常:主要
12-26 22:59:24.133:E / AndroidRuntime(6710):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.jsouptest / com.example.jsouptest.MainActivity}:android.os.NetworkOnMainThreadException

搜索和询问我发现月食看到jsoup.jar,但无法将其打包为APK文件的应用程序运行后。我试图通过组织pressing进口的 Shift + Alt键+ O ,我会得到同样的错误。在这一点上,我不确定什么是错的,而且不知道如何解决它。我只希望有人会导致我走向解决方案。鸭preciate您的时间!

这是我的code:

 包com.example.jsouptest;进口java.io.IOException异常;进口org.jsoup.Jsoup;
进口org.jsoup.nodes.Document;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
         文档文档;
         尝试{
             DOC = Jsoup.connect(http://google.com/)获得();
             字符串title = doc.title();
          System.out.print(职称);
         }赶上(IOException异常五){
             // TODO自动生成catch块
             e.printStackTrace();
         }
         }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }}


解决方案

您有一个

  android.os.NetworkOnMainThreadException

这无关你的罐子工作正常与否。

唯一的例外是由Android的新版本抛出,这样你就不会锁定在UI线程(即主线程)做网络运营。

使用的的AsyncTask (约合什么AsyncTask的每一部分也都可以找到简单的解释<一href=\"http://androidresearch.word$p$pss.com/2012/03/17/understanding-asynctask-once-and-forever/\">here)or另一个线程网络的东西。一个是AsyncTask的更容易使用,如果你需要更新UI工作一旦后台工作完成。

与code基地的AsyncTask的一个例子可以是:

 私有类MyTask扩展的AsyncTask&LT;太虚,太虚,字符串&GT; {  @覆盖
  保护字符串doInBackground(虚空...... PARAMS){
    字符串title =;
    文档文档;
    尝试{
      DOC = Jsoup.connect(http://google.com/)获得();
      标题= doc.title();
      System.out.print(职称);
    }赶上(IOException异常五){
      e.printStackTrace();
    }
    返回称号;
  }
  @覆盖
  保护无效onPostExecute(字符串结果){
    //如果你有一个UI元素,可以显示标题
    ((的TextView)findViewById(R.id.myTextView))的setText(结果)。
  }
}

我假设:


  • 任务是一个在你Activity类。

  • 您的转发已经到UI元素(如的TextView ),因为这就是AsyncTasks通常用于标题。

要调用AsyncTask的,把这个后的setContentView(R.layout.activity_main);

 新MyTask()执行();

I downloaded the jsoup library jsoup-1.7.1.jar core and imported it to my project using the Project -> Properties->Java Build Path -> Add external Jars and I pasted the library file to my libs folder. However there seems to be some problem about importing the Jsoup library to my project. When I run my app, upon launching I get this error.

12-26 22:59:24.133: E/AndroidRuntime(6710): FATAL EXCEPTION: main
12-26 22:59:24.133: E/AndroidRuntime(6710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsouptest/com.example.jsouptest.MainActivity}: android.os.NetworkOnMainThreadException

After searching and asking I found out that eclipse sees the jsoup.jar, but unable to package it to APK file for the app to run. I tried to organize Imports by pressing Shift+Alt+O and I would get the same error. At this point, I am unsure about what is wrong and have no idea how to fix it. I have only hope someone will lead me toward the solution. Appreciate your time!

This is my code:

package com.example.jsouptest;

import java.io.IOException;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Document doc;
         try {
             doc = Jsoup.connect("http://google.com/").get();
             String title = doc.title();
          System.out.print(title);
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

解决方案

You have a

android.os.NetworkOnMainThreadException

Which has nothing to do with your jar working properly or not.

The Exception is thrown by the new versions of Android so that you don't lock the UI Thread (which is the main Thread) doing network operations.

Use an AsyncTask (simple explanation about what each part of the AsyncTask does can be found here)or another Thread for network stuff. An Asynctask is easier to work with if you need to update the UI once the background work completes.

An example of an AsyncTask with your code base can be:

private class MyTask extends AsyncTask<Void, Void, String> {

  @Override
  protected String doInBackground(Void... params) {
    String title ="";
    Document doc;
    try {
      doc = Jsoup.connect("http://google.com/").get();
      title = doc.title();
      System.out.print(title);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return title;   
  } 


  @Override
  protected void onPostExecute(String result) {        
    //if you had a ui element, you could display the title
    ((TextView)findViewById (R.id.myTextView)).setText (result);
  }
}

I assume:

  • The task is an inner class in your Activity.
  • You are forwarding the title gotten to a UI Element (eg a TextView) since that's what AsyncTasks are usually used for.

To call the AsyncTask, put this after setContentView(R.layout.activity_main);

new MyTask().execute();

这篇关于与jsoup解析抛出错误(NetworkOnMainThreadException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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