2012-12-14 82 views
1

我試圖用JSoup連接異步。 我想從URL獲取標題,但發現異常。 但我不明白爲什麼錯誤異步任務doInBackground()&JSoup

我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); 
    } 
    } 
} 
+1

確保jsoup的正確版本是出口在您的應用程序 – njzk2

+0

我用JSOUP(1.7.1) –

回答

1

使用PARAMS作爲數組,params[0] params[1] ...是正確的。

你可以添加一個通用的異常catch,所以看到關於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; 
+1

相關的最終版本的部份問題?這種錯誤將在編譯時檢測到。 – njzk2

+0

好點。讓我更新我的答案。爲什麼編譯器會在編譯過程中找到該方法,但在運行時卻不存在? '無法找到方法org.jsoup.Jsoup.connect,從方法com.example.example.MainActivity引用$ MyTask.doInBackground' – jamis0n

+0

我不知道爲什麼編譯不會除了在這一行。LogCat下劃線爲綠燈 –