【问题标题】:Use RxJava in order to parse an item and create a new one from the previous使用 RxJava 来解析一个项目并从前一个项目创建一个新项目
【发布时间】:2015-08-22 21:37:37
【问题描述】:

我有以下对象,我想使用 RxJava 来创建一个新对象。这背后的逻辑是每篇文章都有很多cmet。它使用 ArticleData.commentId 和 Comment.id 找到正确的 cmets。

public class ArticlesResponse  {
    public List<ArticleData> articles;
    public List<Data> comments;
}

public class Data {
    public int id;
    public String link;
    public String title;
    public String author
    public String body;
}

public class ArticleData extends Data {
        public List<int> commentId;
}

那么我如何使用 Rxjava 来创建以下对象

public class Article extends Data {
        public List<Comments> comments;
}

public class Comments extends Data {
      // comments will have more attributes in the feature
      // so use a seperate object
}

我知道我必须使用flapMap 和过滤器才能解析“ArticleResponse”,但我不知道如何将所有这些放在一起。 此外,“ArticleResponse”是从我从 Retrofit 获得的 json 生成的,所以我想使用 RxJava 会更好,因为我已经有了 Observable 而不是将嵌套的 for 放在我的回调中。

【问题讨论】:

  • 也许你的代码 sn-p 有错误?
  • public class Data extends { 是什么意思?
  • 是的,这是一个错字...它在真实代码中不存在

标签: android retrofit rx-java


【解决方案1】:

我假设您的意思是articlesResponse.cmets 是一个列表,其中包含所有ArticleData 的所有评论,虽然我不认为将这些数据包装在一起并在客户端进行地图操作是一个好主意,这项工作应该在服务器。

我认为你的ArticlesResponse 的comments 字段应该是List&lt;Comments&gt;。

有了这些假设,下面的代码可能会完成你想要的工作(我把它们放在TempTest 类中,并定义你描述的接口,并模拟它以通过 javac 编译,我还使用 Java 8 lambda 语法为了代码简单)。

public class TempTest {

  public static class Data {
    public int id;
    public String link;
    public String title;
    public String author;
    public String body;
  }

  public static class ArticleData extends Data {
    public List<Integer> commentId;
  }

  public static class Comments extends Data {
    // comments will have more attributes in the feature
    // so use a seperate object
  }

  public static class ArticlesResponse {
    public List<ArticleData> articles;
    public List<Comments> comments;
  }

  public class Article extends Data {
    public List<Comments> comments;
  }

  public interface TestInterface {
    Observable<ArticlesResponse> getArticle();
  }

  public static Comments findCommentWithId(int commentId, List<Comments> comments) {
    for (Comments comment : comments) {
        if (comment.id == commentId) {
            return comment;
        }
    }
    return null;
  }

  @Test
  public void simpleTestcase() {
    // assume you means that articlesResponse.comments is a list contains all Comments of these
    // all ArticleData, although I don't think wrap these data together and do the map operation
    // in client is a good idea, this job should be done at server
    TestInterface testInterface = mock(TestInterface.class);
    testInterface.getArticle().map(articlesResponse -> {
        List<Article> result = new ArrayList<>();
        // for each ArticleData in articlesResponse.articles
        for (ArticleData articleData : articlesResponse.articles) {
            // get all Comments from articlesResponse.comments
            Article article = new Article();
            // ... copy Data field from articleData to article
            article.comments = new ArrayList<>();
            for (Integer id : articleData.commentId) {
                Comments comment = findCommentWithId(id, articlesResponse.comments);
                if (comment != null) {
                    article.comments.add(comment);
                }
            }
            result.add(article);
        }
        return result;
    }).subscribe(articles -> {
        for (Article article : articles) {
            System.out.println(article);
        }
    });
  }
}

【讨论】:

  • 非常感谢。这正是我想要的。我知道映射应该在服务器中完成,但这是我们获取 json 的方式,我们无法更改它。
  • @kokeroulis ,如果你觉得我的回答有用,请采纳,谢谢! :)
【解决方案2】:

对您的实际问题有点困惑,所以希望这会有所帮助。 Retrofit 可以为您返回一个 Observable,这应该使 RxJava 集成变得容易。例如,在您的服务中,您可以:

@GET(<your endpoint>)
Observable<ArticlesResponse> getArticles();

然后这样称呼它:

<yourService>.getArticles()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedules.mainThread())
    .subscribe() // manipulate how you want

【讨论】:

  • 好的,让我说得更清楚一点。我有 Observable,如何从这个创建两个新的 observable? (新的对象将从 ArticlesResponse 的数据中填充)第一个是 X 类,第二个是 Y 类。当它们都被创建时,我想将 X 和 Y 合并到为了创建 Z observable。抱歉上面的评论,我错过了点击添加评论...
猜你喜欢
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 2020-11-12
相关资源
最近更新 更多