1

我正在用 Jsoup 开发应用程序。问题是当我在 Getters 的帮助下从其他类调用它时它不起作用。但是当我在 Single Activity 中调用它时它正在运行。我无法找到它为什么不能正常工作,因为它应该。这是包含所有活动的 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

主要活动

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);
    }
}

线形图

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

jsoup法案

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;
    }
}
4

2 回答 2

3

您在本地URL字符串中指定. 如果您尝试从其他活动访问,则该字符串将为空。为了解决它移动AsyncTask类中的字符串,它会工作jsoupAct activity

或者将空的构造函数添加到jsonAct活动中并在其中分配 的值urlonCreate()仅在使用startActivity()

于 2013-11-14T15:00:55.720 回答
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
于 2013-11-14T14:57:57.287 回答