{"id":394,"date":"2023-02-25T08:48:36","date_gmt":"2023-02-25T00:48:36","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=394"},"modified":"2023-04-30T14:37:17","modified_gmt":"2023-04-30T06:37:17","slug":"rxjava2-learning-6-obtain-user-information-after-login","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/rxjava2-learning-6-obtain-user-information-after-login\/","title":{"rendered":"RxJava2\u5b66\u4e60\u4e4b\u516d\uff1a\u767b\u5f55\u540e\u518d\u83b7\u53d6\u7528\u6237\u4fe1\u606f"},"content":{"rendered":"<p>\u767b\u5f55\u540e\u518d\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u4f7f\u7528 FlatMap\u64cd\u4f5c\u7b26\uff0c\u5c06\u7528\u6237\u8bf7\u6c42\u53c2\u6570\u8f6c\u6362\u4e3a\u8fd4\u56de\u54cd\u5e94\u7ed3\u679c\uff0c\u518d\u5c06\u54cd\u5e94\u7ed3\u679c\u8f6c\u6362\u4e3a\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<h2>FlatMap \u64cd\u4f5c\u7b26<\/h2>\n<p><!-- more --><\/p>\n<p>FlatMap \u5c06\u4e00\u4e2a\u53d1\u5c04\u6570\u636e\u7684Observable\u53d8\u6362\u4e3a\u591a\u4e2aObservables\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u53d1\u5c04\u7684\u6570\u636e\u5408\u5e76\u540e\u653e\u8fdb\u4e00\u4e2a\u5355\u72ec\u7684Observable\u3002<\/p>\n<p>FlatMap \u64cd\u4f5c\u7b26\u4f7f\u7528\u4e00\u4e2a\u6307\u5b9a\u7684\u51fd\u6570\u5bf9\u539f\u59cbObservable\u53d1\u5c04\u7684\u6bcf\u4e00\u9879\u6570\u636e\u6267\u884c\u53d8\u6362\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u672c\u8eab\u4e5f\u53d1\u5c04\u6570\u636e\u7684Observable\uff0c\u7136\u540e FlatMap \u5408\u5e76\u8fd9\u4e9bObservables\u53d1\u5c04\u7684\u6570\u636e\uff0c\u6700\u540e\u5c06\u5408\u5e76\u540e\u7684\u7ed3\u679c\u5f53\u505a\u5b83\u81ea\u5df1\u7684\u6570\u636e\u5e8f\u5217\u53d1\u5c04\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5f53\u4f60\u6709\u4e00\u4e2a\u8fd9\u6837\u7684Observable\uff1a\u5b83\u53d1\u5c04\u4e00\u4e2a\u6570\u636e\u5e8f\u5217\uff0c\u8fd9\u4e9b\u6570\u636e\u672c\u8eab\u5305\u542bObservable\u6210\u5458\u6216\u8005\u53ef\u4ee5\u53d8\u6362\u4e3aObservable\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Observable\u53d1\u5c04\u8fd9\u4e9b\u6b21\u7ea7Observable\u53d1\u5c04\u7684\u6570\u636e\u7684\u5b8c\u6574\u96c6\u5408\u3002<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1aFlatMap \u5bf9\u8fd9\u4e9bObservables\u53d1\u5c04\u7684\u6570\u636e\u505a\u7684\u662f\u5408\u5e76( merge )\u64cd\u4f5c\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u80fd\u662f\u4ea4\u9519\u7684\u3002<\/p>\n<\/blockquote>\n<pre><code class=\"language-java\">Retrofit retrofit = new Retrofit.Builder()\n        .baseUrl(SERVER_ADDRESS)\n        .addConverterFactory(GsonConverterFactory.create())\n        .build();\napi = retrofit.create(Api.class);\n\nUserParam param = new UserParam(&quot;appblog.cc&quot;, &quot;123456&quot;);\n\nObservable.just(param).flatMap(new Function&lt;UserParam, ObservableSource&lt;ResponseResult&gt;&gt;() {\n    @Override\n    public ObservableSource&lt;ResponseResult&gt; apply(UserParam userParam) throws Exception {\n        ResponseResult result = api.login(userParam).execute().body();\n        return Observable.just(result);\n        \/\/\u9700\u8981\u6dfb\u52a0Retrofit\u9002\u914d\u5668adapter-rxjava\u548caddCallAdapterFactory(RxJavaCallAdapterFactory.create())\n        \/\/return api.loginWithRx(userParam);\n    }\n}).flatMap(new Function&lt;ResponseResult, ObservableSource&lt;User&gt;&gt;() {\n    @Override\n    public ObservableSource&lt;User&gt; apply(ResponseResult responseResult) throws Exception {\n        User user = api.getUserInfoWithPath(responseResult.getUserId()).execute().body();\n        return Observable.just(user);\n    }\n}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer&lt;User&gt;() {\n    @Override\n    public void accept(User user) throws Exception {\n        Toast.makeText(MainActivity.this, user.getUsername(), Toast.LENGTH_SHORT).show();\n    }\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u767b\u5f55\u540e\u518d\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u4f7f\u7528 FlatMap\u64cd\u4f5c\u7b26\uff0c\u5c06\u7528\u6237\u8bf7\u6c42\u53c2\u6570\u8f6c\u6362\u4e3a\u8fd4\u56de\u54cd\u5e94\u7ed3\u679c\uff0c\u518d\u5c06\u54cd\u5e94\u7ed3\u679c\u8f6c\u6362\u4e3a\u7528\u6237\u4fe1\u606f\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131],"tags":[132],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-rxjava","tag-rxjava"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=394"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/394\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}