gpt4 book ai didi

android - 错误异步任务 doInBackground() & JSoup

转载 作者:行者123 更新时间:2023-11-30 03:52:53 25 4
gpt4 key购买 nike

我尝试将 Async 与 JSoup 连接起来。我想从 URL 中获取标题,但遇到异常。但是我不明白为什么

我的 LogCat 是:

    12-14 23:54:19.612: I/Process(568): Sending signal. PID: 568 SIG: 9
12-14 23:54:55.141: D/gralloc_goldfish(632): Emulator without GPU emulation detected.
12-14 23:55:01.051: I/dalvikvm(632): Could not find method org.jsoup.Jsoup.connect, referenced from method com.example.myexample.MainActivity$MyTask.doInBackground
12-14 23:55:01.081: W/dalvikvm(632): VFY: unable to resolve static method 3465: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
12-14 23:55:01.081: D/dalvikvm(632): VFY: replacing opcode 0x71 at 0x000a
12-14 23:55:03.111: W/dalvikvm(632): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
12-14 23:55:03.181: E/AndroidRuntime(632): FATAL EXCEPTION: AsyncTask #1
12-14 23:55:03.181: E/AndroidRuntime(632): java.lang.RuntimeException: An error occured while executing doInBackground()
12-14 23:55:03.181: E/AndroidRuntime(632): at android.os.AsyncTask$3.done(AsyncTask.java:278)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-14 23:55:03.181: E/AndroidRuntime(632): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.lang.Thread.run(Thread.java:856)
12-14 23:55:03.181: E/AndroidRuntime(632): Caused by: java.lang.NoClassDefFoundError: org.jsoup.Jsoup
12-14 23:55:03.181: E/AndroidRuntime(632): at com.example.myexample.MainActivity$MyTask.doInBackground(MainActivity.java:50)
12-14 23:55:03.181: E/AndroidRuntime(632): at com.example.myexample.MainActivity$MyTask.doInBackground(MainActivity.java:1)
12-14 23:55:03.181: E/AndroidRuntime(632): at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-14 23:55:03.181: E/AndroidRuntime(632): ... 5 more

据我所知,doInBackground 部分有误。当我将 params[0] 更改为 params 时,我也有错误

public class MainActivity extends Activity {
MyTask mt;
TextView tvInfo;
String URL="http://www.yandex.ru/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvInfo = (TextView) findViewById(R.id.tvInfo);
}

public void onclick(View v) {
mt = new MyTask();
mt.execute(URL);
}

class MyTask extends AsyncTask<String, Void, String> {
Document doc;
String title=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
tvInfo.setText("Please wait");
}

@Override
protected String doInBackground(String... params) {
try {
TimeUnit.SECONDS.sleep(2);
doc = Jsoup.connect(params[0]).get();
String title = doc.title();
Log.d("AsyncTask doInBackground","URL: " + params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return title;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvInfo.setText(title);
}
}
}

最佳答案

使用 params 作为数组,params[0] params[1] ... 是正确的。

您能否添加一般异常捕获以便查看有关 JSoup 连接错误的更多细节?

此外,记录 params[0] 的值以确保它是一个有效的 URL:

Log.d("AsyncTask doInBackground","URL: " + params[0]);

虽然如果没有它们您的项目将无法编译,但请确保您正在导入正确的 Java JSoup 库。你的错误说它找不到静态方法 connection

import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

关于android - 错误异步任务 doInBackground() & JSoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13882046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com