在RxJava Observable中制作Facebook图形请求

时间:2015-11-09 12:51:59

标签: android facebook-graph-api rx-java

我有一种使用Facebook Android SDK异步获取当前用户信息的方法。但是当检索到用户信息时,我必须将个人资料图片URL转换为位图,我也必须异步执行此操作。所以要下载我使用RxJava的个人资料照片:

public void getCurrentUserInfo(){

    GraphRequest graphRequest = GraphRequest.newMeRequest(fbAccessToken,
            new GraphRequest.GraphJSONObjectCallback() {

                @Override
                public void onCompleted(final JSONObject facebookUser, GraphResponse response) {

                    try {

                        final UserInfo userInfo = new UserInfo();
                        userInfo.setFacebookId(facebookUser.getString("id"));
                        userInfo.setName(facebookUser.getString("name"));
                        userInfo.setEmail(facebookUser.getString("email"));
                        userInfo.setGender(facebookUser.getString("gender"));


                        //RxJava Observable subscription
                        downloadProfilePicture(facebookUser.getJSONObject("picture")
                                                           .getJSONObject("data")
                                                           .getString("url"))
                                              .subscribe(new Action1<Bitmap>() {
                        @Override
                        public void call (Bitmap bitmap){

                            userInfo.setProfilePic(bitmap);

                            });


                    }catch (JSONException jsonException){

                        jsonException.printStackTrace();

                    }
                }
            });

    Bundle parameters = new Bundle();
    parameters.putString("fields", requestFields);
    graphRequest.setParameters(parameters);
    graphRequest.executeAsync();
}

我的观察:

 public Observable<Bitmap> downloadProfilePicture(final String... url){

   return Observable.create(new Observable.OnSubscribe<Bitmap>() {

       @Override
       public void call(Subscriber<? super Bitmap> subscriber) {

           if (!subscriber.isUnsubscribed()) { //2

               try {

                   InputStream inputStream = new java.net.URL(url[0]).openStream();
                   Bitmap profilePic = BitmapFactory.decodeStream(inputStream);

                   subscriber.onNext(profilePic); //4
                   subscriber.onCompleted(); //5

               } catch (IOException e) {

                   subscriber.onError(e); //6
               }
           }
       } //1
   }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}

我的问题是,有没有办法创建一个Observable,使GraphRequest和结果自动下载个人资料图片? (也许使用运算符?)

1 个答案:

答案 0 :(得分:1)

Imho,除了我的评论之外,还有一些代码可以让它变得更漂亮:

GraphRequest request = new GraphRequest(fbAccessToken, "/me");
Observable.just(request.executeAndWait())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .map(new Func1<GraphResponse, UserInfo>() {
                @Override
                public UserInfo call(GraphResponse graphResponse) {
//parse and download your bitmap here
                    return parseGraphResponse(graphResponse);
                }
            })
            .subscribe(new Subscriber<UserInfo>() {
...}

说实话,我还没有测试过这段代码。这就是我使用facebook sdk(4.6.0)获取Facebook的节点所在地点的方式&#34; / search&#34;。但也许你有个主意。