Android中的Jsoup,因NoClassDefFound错误而崩溃

时间:2015-04-28 07:07:56

标签: android parsing jsoup

我正在尝试在HTML网页中加载一个部门,所以首先我开始使用带有分区的简单HTML代码...来提取我试图使用Jsoup.parse()方法解析HTML字符串的部门但是它不管用。我已经在项目中添加了Jsoup库。但每当我 尝试使用

解析它
Document doc = Jsoup.parse(str);

 doc = Jsoup.connect(str).get();`

显示Application has unexpectedly stopped working。 我不知道问题出在哪里。

package com.example.kdp;


import java.io.IOException;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.parser.Parser;

import org.jsoup.select.Elements;

import org.jsoup.nodes.Element;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class Search extends Activity{

    Button bSearch;
    EditText tSearch;
    TextView showText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        bSearch = (Button) findViewById(R.id.bSearch);
        tSearch = (EditText) findViewById(R.id.tSearch);
        showText = (TextView) findViewById(R.id.show);
        bSearch.setOnClickListener(new View.OnClickListener() {


            @SuppressLint("SetJavaScriptEnabled")
            public void onClick(View v) {   


                final WebView web = (WebView) findViewById(R.id.webview);   
                web.setWebViewClient(new WebViewClient());
                web.getSettings().setJavaScriptEnabled(true);
                web.getSettings().setLoadWithOverviewMode(true);
                web.getSettings().setLightTouchEnabled(true);
                web.getSettings().setUseWideViewPort(true);             
                new Thread(new Runnable() {
                       @Override
                       public void run() {                         

                           web.post(new Runnable() {
                               @Override
                               public void run() {                                                      
                                    String str="<html><div class='subfooter'>Total expense:$100</div><div class='subheading'>Total revenue:$32</div></html>";
                                    Document doc = Jsoup.parse(str);
                                    //Document doc = Jsoup.connect(str).get();
                                    showText.setText(str);                          
                                    web.loadData(str,"html/text","utf-8");

                              }
                          });
                      }
                  }).start();                 

            }
        });


    }


}

logcat的:

04-28 16:47:56.391: I/dalvikvm(535): Could not find method 

org.jsoup.Jsoup.connect,
referenced from method com.example.kdp.Search$1$1.run 
04-28 16:47:56.391: W/dalvikvm(535): VFY: unable to resolve static method 3591: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection; 04-28 16:47:56.421: W/dalvikvm(535): threadid=12: thread exiting with uncaught exception (group=0x40015560) 
04-28 16:47:56.421: E/AndroidRuntime(535): FATAL EXCEPTION: Thread-14 04-28 16:47:56.421: E/AndroidRuntime(535): java.lang.NoClassDefFoundError: org.jsoup.Jsoup 
04-28 16:47:56.421: E/AndroidRuntime(535)

1 个答案:

答案 0 :(得分:1)

请使用

showText.setText(Html.fromHtml(str));    

而不是

showText.setText(str);   

因为Html.fromHtml会给你你所期望的。