2013-11-14 160 views
0

我正在用Jsoup開發應用程序。問題是,當我在Getters的幫助下從其他課程調用它時,它不起作用。但是當我在單一活動中調用它時,它正在運行。我無法找到它爲什麼無法正常工作,因爲它應該。 以下是包含所有活動的logCat文件。AsyncTask#1:執行doInBackground()時發生錯誤

logcat的

11-14 20:16:52.063: E/AndroidRuntime(1871): FATAL EXCEPTION: AsyncTask #1 
11-14 20:16:52.063: E/AndroidRuntime(1871): java.lang.RuntimeException: An error occured while executing doInBackground() 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at android.os.AsyncTask$3.done(AsyncTask.java:299) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at java.lang.Thread.run(Thread.java:856) 
11-14 20:16:52.063: E/AndroidRuntime(1871): Caused by: java.lang.IllegalArgumentException: Must supply a valid URL 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at org.jsoup.helper.Validate.notEmpty(Validate.java:102) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at org.jsoup.helper.HttpConnection.url(HttpConnection.java:57) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:27) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at org.jsoup.Jsoup.connect(Jsoup.java:73) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at com.example.frgbdf.jsoupAct$Parsee.doInBackground(jsoupAct.java:30) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at com.example.frgbdf.jsoupAct$Parsee.doInBackground(jsoupAct.java:1) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
11-14 20:16:52.063: E/AndroidRuntime(1871):  ... 4 more 

MainActivity

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent i = new Intent(getApplicationContext(), LineGraph.class); 
     startActivity(i); 
    } 
} 

LineGraph

public class LineGraph extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     jsoupAct mJsoupAct = new jsoupAct(); 
     String parseStrings; 
     parseStrings = mJsoupAct.getOutput(); 
     Log.d("xstring", parseStrings + ""); 
    } 
} 

jsoupAct

public class jsoupAct extends Activity { 
    String output = "00"; 
    String url; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     url = "www.google.com"; 
    } 

    public void mExecute() { 
     new Parsee().execute(); 

    } 

    public class Parsee extends AsyncTask<String, String, String> { 

     protected String doInBackground(String... params) { 
      try { 
       Document doc = Jsoup.connect(url).get(); 
       String body = doc.body().text(); 
       output = body.toString(); 
      } catch (IOException e) { 

       e.printStackTrace(); 
      } 
      return output; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(output); 
     } 

    } 

    public String getOutput() { 
     mExecute(); 
     return output; 
    } 

    public void setOutput(String output) { 
     this.output = output; 
    } 
} 
+0

MB網址開始活動時纔會被調用? – Suvitruf

+0

嘗試使用'http:// www.google.com'添加http部分 – Coderji

回答

3

你正在一個字符串本地jsoupAct activity指定URL。如果您嘗試從其他活動進行訪問,該字符串將爲空。爲了解決其移動字符串中的AsyncTask類中,它會工作

或空的構造函數添加到jsonAct活動和其分配的url值。 onCreate()僅當使用「http」而不是「www」使用startActivity()

2

您已經回答了自己的問題:

Caused by: java.lang.IllegalArgumentException: Must supply a valid URL 
    at org.jsoup.helper.Validate.notEmpty(Validate.java:102) 
    at org.jsoup.helper.HttpConnection.url(HttpConnection.java:57) 
    at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:27) 
    at org.jsoup.Jsoup.connect(Jsoup.java:73) 
    at com.example.frgbdf.jsoupAct$Parsee.doInBackground(jsoupAct.java:30) 
    at com.example.frgbdf.jsoupAct$Parsee.doInBackground(jsoupAct.java:1) 
    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
    ... 4 more 
+1

重新發布logcat絕不是答案。 OP顯然不知道爲什麼*錯誤正在發生或者如何解決。 – codeMagic

+0

是的。但是,這是一些如何指向我失蹤的路線。 – Aexyn

相關問題