解析Html时Jsoup发出错误

时间:2013-01-19 00:29:38

标签: android html parsing

代码在java控制台中工作正常但在android模拟器中它会出错。我检查了同类型的其他帖子但没有成功......

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

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

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Geocoder;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidGPSTrackingActivity extends Activity {
//some code which calls directions

    }



    public void directions() {
        try {

                        Document doc = Jsoup.connect("http://10.0.2.2/kj/jhjh.htm")
                    .get();////////////////error occurs here
            Elements el = doc.getElementsByClass("lkklk");
            String str = el.text();
            str = str.replaceAll("[0-9]+[.] ", "\n");
            string = str.split("\n");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

以下是错误......

01-19 05:34:35.723: E/AndroidRuntime(952): FATAL EXCEPTION: main
01-19 
05:34:35.723: E/AndroidRuntime(952): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.example.gpstracking.AndroidGPSTrackingActivity.directions(AndroidGPSTrackingActivity.java:308)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.example.gpstracking.AndroidGPSTrackingActivity.onActivityResult(AndroidGPSTrackingActivity.java:193)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.Activity.dispatchActivityResult(Activity.java:5192)

01-19 05:34:35.723: E/AndroidRuntime(952):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.ActivityThread.access$1100(ActivityThread.java:130)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.os.Looper.loop(Looper.java:137)

01-19 05:34:35.723: E/AndroidRuntime(952):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at java.lang.reflect.Method.invoke(Method.java:511)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您获得NoClassDefFoundError,因为您的jar文件在运行时不可用。为了使它在运行时可用,你必须将它放在你的libs文件夹中并检查你的java构建路径中的jar文件的复选框,如下所示:

enter image description here