Converting this AsyncTask to RxAndroid?

时间:2016-02-03 02:58:21

标签: android android-asynctask rx-java rx-android

Bit new to Rx, so am looking for some help on converting the following AsyncTask to Rx, hopefully so I can visualize Rx a bit more with code that I already know that does something. I've found a few other SO answers that were somewhat relevant, but alot of them werent network requests and many used different operators for different answers, so am a bit confused.

Heres the AsyncTask:

public class BackgroundStuff extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        UserAgent myUserAgent = UserAgent.of("xxx:xxxx:xxx");

        RedditClient redditClient = new RedditClient(myUserAgent);
        Credentials credentials = Credentials.userlessApp("xxxxxxxx", UUID.randomUUID());
        OAuthData authData = null;
        try {
            authData = redditClient.getOAuthHelper().easyAuth(credentials);
            redditClient.authenticate(authData);
        } catch (OAuthException e) {
            e.printStackTrace();
        }

        SubredditPaginator sp = new SubredditPaginator(redditClient);

        sp.setLimit(7);
        sp.setSorting(Sorting.HOT);
        sp.setTimePeriod(TimePeriod.DAY);

        sp.setSubreddit("pics");

        sp.next(true);
        Listing<Submission> list = sp.getCurrentListing();

        Submission sub = list.get(1);
        for (Submission submission : list) {
            Log.d("test", "title: " + submission.getTitle());
        }
        Submission sub2 = redditClient.getSubmission(sub.getId());
        System.out.println(sub2.getComments().get(0).getComment().getBody());


        return null;
    }
}

For reference, this authenticates to Reddit and then grabs some results from a particular subreddit.

I know I need an Observable, and then .suscribeOn(Schedulers.newThread()) and .observeOn(AndroidSchedulers.mainThread()), but am not sure how to make the observable.

1 个答案:

答案 0 :(得分:2)

将其包裹在功能中:

    Void doSomething(){
    UserAgent myUserAgent = UserAgent.of("xxx:xxxx:xxx");

    RedditClient redditClient = new RedditClient(myUserAgent);
    Credentials credentials = Credentials.userlessApp("xxxxxxxx", UUID.randomUUID());
    OAuthData authData = null;
    try {
        authData = redditClient.getOAuthHelper().easyAuth(credentials);
        redditClient.authenticate(authData);
    } catch (OAuthException e) {
        e.printStackTrace();
    }

    SubredditPaginator sp = new SubredditPaginator(redditClient);

    sp.setLimit(7);
    sp.setSorting(Sorting.HOT);
    sp.setTimePeriod(TimePeriod.DAY);

    sp.setSubreddit("pics");

    sp.next(true);
    Listing<Submission> list = sp.getCurrentListing();

    Submission sub = list.get(1);
    for (Submission submission : list) {
        Log.d("test", "title: " + submission.getTitle());
    }
    Submission sub2 = redditClient.getSubmission(sub.getId());
    System.out.println(sub2.getComments().get(0).getComment().getBody());


    return null;
    }

将其包裹在一个可观察的地方:

Observable.just(doSomething);

以延迟方式包裹它,以便只有在有人订阅它时它才会运行:

Observable<Void> doSomethingObservable = Observable.defer(()-> Observable.just(doSomething))

决定使用subscribeOn和observeOn运行它的位置,并订阅它以使其运行:

doSomethingObservable
.suscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread()).subscribe();