如何在两个Activity中使用相同的代码JSOUP

时间:2014-08-25 21:20:48

标签: java android jsoup

我想在两个Activity中使用JSOUP,但两者都有相同的代码。

public class MainActivity extends Activity {

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

        Button titlebutton = (Button) findViewById(R.id.button1);

        titlebutton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // Execute Title AsyncTask
                new AsyncTest().execute();
            }
        });


    }

    private class AsyncTest extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            Connection.Response response = null;
            try {
                response = Jsoup.connect("http://example.com")
                            .data("param1", "aaaa")
                            .data("param2", "bbbb")
                            .data("param3", "ccc")
                            .data("param4", "dd")
                            .execute();

            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }
}

public class Second extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        get();
    }

    public Connection.Response get()
    {
        Connection.Response response = null;
        try {
            response = Jsoup.connect("http://example.com")
                        .data("param1", "aaaa")
                        .data("param2", "bbbb")
                        .data("param3", "ccc")
                        .data("param4", "dd")
                        .execute();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }
}

这只是一个例子 - 我在第二个Activity中使用了很多JSOUP操作,所以我想在这个类中使用函数get并将其导入MainActivity。

我在MainActivity中尝试:

private class AsyncTest extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Connection.Response response = null;
        response = new Second().get();

        return null;
    }
}

但是这会返回错误:

08-25 21:18:00.164: W/dalvikvm(1552): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
08-25 21:18:00.174: E/AndroidRuntime(1552): FATAL EXCEPTION: AsyncTask #1
08-25 21:18:00.174: E/AndroidRuntime(1552): java.lang.RuntimeException: An error occured while executing doInBackground()
08-25 21:18:00.174: E/AndroidRuntime(1552):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.lang.Thread.run(Thread.java:856)
08-25 21:18:00.174: E/AndroidRuntime(1552): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-25 21:18:00.174: E/AndroidRuntime(1552):     at android.os.Handler.<init>(Handler.java:121)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at android.app.Activity.<init>(Activity.java:735)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at com.example.js.Second.<init>(Second.java:11)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at com.example.js.MainActivity$AsyncTest.doInBackground(MainActivity.java:35)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at com.example.js.MainActivity$AsyncTest.doInBackground(MainActivity.java:1)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-25 21:18:00.174: E/AndroidRuntime(1552):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-25 21:18:00.174: E/AndroidRuntime(1552):     ... 5 more

在我的示例中,非重复代码的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

将您的AsyncTask公开并将其移至单独的类(文件):

<强> AsyncTest.java:

public class AsyncTest extends AsyncTask<Void, Void, Void> {
    ...
}

然后在你的活动中致电:

new AsyncTest().execute();