我正在尝试获取网页的html并使用android中的jsoup查找此页面中显示的所有链接。我已将jsoup添加为外部lib。甚至尝试在libs文件夹中添加它。但我得到“不幸的是app停止”错误。任何人都可以帮助我..
这是我的MainActivity java代码。
package com.example.asyncjsoup;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textv=(TextView)findViewById(R.id.textView1);
new DownloadTask(textv).execute("http://www.karnatakatourism.org/");
//Elements links=(Elements)link;
//tv1.setText((CharSequence) b);
}
@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;
}
}
这是我定义异步任务的类。
package com.example.asyncjsoup;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
@SuppressLint("NewApi")
public class DownloadTask extends AsyncTask<String, Void, Elements>{
private TextView view;
public DownloadTask(TextView textvi)
{
this.view=textvi;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//pDialog = new ProgressDialog(MainActivity.this);
// pDialog.setMessage("Loading...");
// pDialog.setIndeterminate(false);
// pDialog.setCancelable(false);
// pDialog.show();
}
@SuppressLint("NewApi")
protected Elements doInBackground(String... urls) {
String url = urls[0];
Document doc;
Elements links=null;
try {
//view.setText("this will be displayed in textview ");
doc = Jsoup.connect(url).get();
//view.setText("this wont be displayed in textview..i get unfortunately app stopped");
links = doc.select("a[href]");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return links;
}
protected void onPostExecute(Elements result) {
print("\nLinks: (%d)", result.size());
for (Element link : result) {
view.setText((CharSequence) link);
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
}
private static void print(String msg, Object... args) {
System.out.println(String.format(msg, args));
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
else
return s;
}
}
我已将清洁文件添加到我的清单文件中。
这是我的logcat ..
01-27 09:35:12.303: E/Trace(1293): error opening trace file: No such file or directory (2)
01-27 09:35:12.303: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:12.303: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:12.303: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:12.723: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:12.763: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.533: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.533: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.543: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.573: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.704: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.713: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.723: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.863: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.863: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.875: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.875: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.875: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.875: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:13.913: D/gralloc_goldfish(1293): Emulator without GPU emulation detected.
01-27 09:35:13.993: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:14.053: W/Trace(1293): Unexpected value from nativeGetEnabledTags: 0
01-27 09:35:15.704: D/dalvikvm(1293): GC_CONCURRENT freed 113K, 9% free 2653K/2888K, paused 78ms+4ms, total 143ms
01-27 09:35:15.704: D/dalvikvm(1293): WAIT_FOR_CONCURRENT_GC blocked 55ms
01-27 09:35:18.573: D/dalvikvm(1293): GC_FOR_ALLOC freed 333K, 19% free 2617K/3192K, paused 35ms, total 35ms
01-27 09:35:18.623: D/dalvikvm(1293): GC_FOR_ALLOC freed 118K, 17% free 2676K/3192K, paused 47ms, total 47ms
01-27 09:35:19.423: D/dalvikvm(1293): GC_CONCURRENT freed 268K, 13% free 2806K/3192K, paused 82ms+81ms, total 225ms
01-27 09:35:19.943: D/dalvikvm(1293): GC_CONCURRENT freed 99K, 7% free 3115K/3336K, paused 72ms+73ms, total 203ms
01-27 09:35:20.713: D/dalvikvm(1293): GC_CONCURRENT freed 287K, 12% free 3213K/3616K, paused 72ms+83ms, total 224ms
01-27 09:35:21.493: D/dalvikvm(1293): GC_CONCURRENT freed 224K, 10% free 3385K/3728K, paused 73ms+84ms, total 226ms
01-27 09:35:22.383: D/dalvikvm(1293): GC_CONCURRENT freed 247K, 10% free 3528K/3888K, paused 77ms+76ms, total 245ms
01-27 09:35:23.244: D/dalvikvm(1293): GC_CONCURRENT freed 249K, 10% free 3674K/4040K, paused 84ms+95ms, total 281ms
01-27 09:35:23.613: I/System.out(1293): Links: (152)
01-27 09:35:23.613: D/AndroidRuntime(1293): Shutting down VM
01-27 09:35:23.623: W/dalvikvm(1293): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-27 09:35:23.644: E/AndroidRuntime(1293): FATAL EXCEPTION: main
01-27 09:35:23.644: E/AndroidRuntime(1293): java.lang.ClassCastException: org.jsoup.nodes.Element cannot be cast to java.lang.CharSequence
01-27 09:35:23.644: E/AndroidRuntime(1293): at com.example.asyncjsoup.DownloadTask.onPostExecute(DownloadTask.java:63)
01-27 09:35:23.644: E/AndroidRuntime(1293): at com.example.asyncjsoup.DownloadTask.onPostExecute(DownloadTask.java:1)
01-27 09:35:23.644: E/AndroidRuntime(1293): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-27 09:35:23.644: E/AndroidRuntime(1293): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-27 09:35:23.644: E/AndroidRuntime(1293): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-27 09:35:23.644: E/AndroidRuntime(1293): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 09:35:23.644: E/AndroidRuntime(1293): at android.os.Looper.loop(Looper.java:137)
01-27 09:35:23.644: E/AndroidRuntime(1293): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-27 09:35:23.644: E/AndroidRuntime(1293): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 09:35:23.644: E/AndroidRuntime(1293): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 09:35:23.644: E/AndroidRuntime(1293): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-27 09:35:23.644: E/AndroidRuntime(1293): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-27 09:35:23.644: E/AndroidRuntime(1293): at dalvik.system.NativeStart.main(Native Method)
01-27 09:35:28.113: I/Process(1293): Sending signal. PID: 1293 SIG: 9
01-27 10:02:31.024: E/Trace(1490): error opening trace file: No such file or directory (2)
01-27 10:02:31.024: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:31.024: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:31.024: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:31.546: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:31.583: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.335: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.335: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.353: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.374: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.553: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.563: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.573: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.768: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.774: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.804: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.804: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.813: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.813: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:32.833: D/gralloc_goldfish(1490): Emulator without GPU emulation detected.
01-27 10:02:32.943: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:33.073: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:34.673: D/dalvikvm(1490): GC_CONCURRENT freed 90K, 8% free 2782K/2996K, paused 85ms+6ms, total 151ms
01-27 10:02:37.123: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:37.123: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:37.283: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:37.283: W/Trace(1490): Unexpected value from nativeGetEnabledTags: 0
01-27 10:02:37.703: D/dalvikvm(1490): GC_FOR_ALLOC freed 451K, 21% free 2677K/3348K, paused 38ms, total 38ms
01-27 10:02:38.513: D/dalvikvm(1490): GC_CONCURRENT freed 267K, 17% free 2800K/3348K, paused 76ms+76ms, total 210ms
01-27 10:02:39.064: D/dalvikvm(1490): GC_CONCURRENT freed 100K, 8% free 3093K/3348K, paused 76ms+84ms, total 220ms
01-27 10:02:39.773: D/dalvikvm(1490): GC_CONCURRENT freed 278K, 11% free 3207K/3600K, paused 76ms+78ms, total 227ms
01-27 10:02:40.573: D/dalvikvm(1490): GC_CONCURRENT freed 223K, 10% free 3378K/3716K, paused 78ms+89ms, total 236ms
01-27 10:02:41.393: D/dalvikvm(1490): GC_CONCURRENT freed 246K, 10% free 3522K/3884K, paused 82ms+90ms, total 258ms
01-27 10:02:42.083: D/dalvikvm(1490): GC_CONCURRENT freed 253K, 10% free 3665K/4036K, paused 76ms+80ms, total 247ms
01-27 10:02:42.443: I/System.out(1490): Links: (152)
01-27 10:02:42.443: D/AndroidRuntime(1490): Shutting down VM
01-27 10:02:42.443: W/dalvikvm(1490): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-27 10:02:42.475: E/AndroidRuntime(1490): FATAL EXCEPTION: main
01-27 10:02:42.475: E/AndroidRuntime(1490): java.lang.ClassCastException: org.jsoup.nodes.Element cannot be cast to java.lang.CharSequence
01-27 10:02:42.475: E/AndroidRuntime(1490): at com.example.asyncjsoup.DownloadTask.onPostExecute(DownloadTask.java:63)
01-27 10:02:42.475: E/AndroidRuntime(1490): at com.example.asyncjsoup.DownloadTask.onPostExecute(DownloadTask.java:1)
01-27 10:02:42.475: E/AndroidRuntime(1490): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-27 10:02:42.475: E/AndroidRuntime(1490): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-27 10:02:42.475: E/AndroidRuntime(1490): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-27 10:02:42.475: E/AndroidRuntime(1490): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 10:02:42.475: E/AndroidRuntime(1490): at android.os.Looper.loop(Looper.java:137)
01-27 10:02:42.475: E/AndroidRuntime(1490): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-27 10:02:42.475: E/AndroidRuntime(1490): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 10:02:42.475: E/AndroidRuntime(1490): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 10:02:42.475: E/AndroidRuntime(1490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-27 10:02:42.475: E/AndroidRuntime(1490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-27 10:02:42.475: E/AndroidRuntime(1490): at dalvik.system.NativeStart.main(Native Method)
01-27 10:02:45.373: I/Process(1490): Sending signal. PID: 1490 SIG: 9
答案 0 :(得分:0)
将jsoup.jar放入“libs”目录中。清理项目并再试一次。有时在项目之外引用库时会出现问题。
也不要将此行放在doInBackground中。您只能在onPostExecute中更改此文本字段。
//view.setText("this wont be displayed in textview..i get unfortunately app stopped");
答案 1 :(得分:0)
从日志的以下部分可以清楚地了解您遇到此错误的原因
01-27 10:02:42.475: E/AndroidRuntime(1490): java.lang.ClassCastException: org.jsoup.nodes.Element cannot be cast to java.lang.CharSequence
01-27 10:02:42.475: E/AndroidRuntime(1490): at com.example.asyncjsoup.DownloadTask.onPostExecute(DownloadTask.java:63)
01-27 10:02:42.475: E/AndroidRuntime(1490): at com.example.asyncjsoup.DownloadTask.onPostExecute(DownloadTask.java:1)
这不是添加Jsoup库的问题,因为你在代码中使用了这个库中的类而eclipse没有提到错误所以如果它是库链接错误那么eclipse已经警告过你了...
您的日志输出清楚地表明您正面临ClassCastException。从这个异常的名称可以清楚地看到,您正试图将一个类的对象转换为另一个不兼容的类的引用。并且它还给出了导致此错误的转换的情况
org.jsoup.nodes.Element cannot be cast to java.lang.CharSequence
这意味着您正在尝试将Element类型的对象(org.jsoup.nodes.Element)强制转换为CharSequence(java.lang.CharSequence)类型的引用,这些引用彼此不兼容。从这里兼容我的意思是这里没有父子关系或接口实现允许转换。
有关下线错误的详细信息
01-27 10:02:42.475: E/AndroidRuntime(1490): at com.example.asyncjsoup.DownloadTask.onPostExecute(DownloadTask.java:63)
这一行清楚地表明,在第63行的文件“DownloadTask.java”中,您将面临此ClassCastException。从上面发布的DownloadTask.java代码中你可以清楚地看到第63行正在关注(我刚刚在你的代码中添加了数字以供解释)
62 for (Element link : result) {
63 view.setText((CharSequence) link);
64 print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
65 }
从这里你可以很容易地看到你正在尝试将名为link(类元素)的对象转换为不兼容的CharSequence引用。如果你想显示那个节点的文本,那么你必须找到正确的类元素方法,并记住这里这个Element类不是DOM包我想你在这里误解它是org.jsoup.nodes.Element(你可以看到)来自类DownloadRask顶部的import语句。因此,要显示文本,您必须阅读文档以找到可在该节点返回正确文本字符串的正确方法。
这里是从快速视图链接到类org.jsoup.nodes.Element http://jsoup.org/apidocs/org/jsoup/nodes/Element.html的文档我可以说Element类的data()或ownText()方法返回你想要的字符串。因此,如果您将第63行的代码修改为以下内容,那么您可以获得所需的内容而不会出现错误
view.setText(link.data());
OR
view.setText(link.ownText());
答案 2 :(得分:0)
01-27 09:35:23.64`enter code here`4: E/AndroidRuntime(1293): java.lang.ClassCastException: org.jsoup.nodes.Element cannot be cast to java.lang.CharSequence
这告诉你不能直接将Element
(jsoup.nodes)转换为CharSequnecne
。
但是,您可以将Element
转换为string
,这可以直接用作CharSequence
而无需任何演员;
因此,将onPostExecute更改为此类
protected void onPostExecute(Elements result)
{
print("\nLinks: (%d)", result.size());
for (Element link : result)
{
//view.setText((CharSequence) link);
view.setText(link.toString());
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
}