Android 2/RxJava2/Room-简单数据处理
我正在开发应用程序,在应用程序启动时,我从Rest服务下载类别和帖子,以便将它们存储在SQLite数据库中。我有几个问题:Android 2/RxJava2/Room-简单数据处理,java,android,retrofit2,rx-java2,android-room,Java,Android,Retrofit2,Rx Java2,Android Room,我正在开发应用程序,在应用程序启动时,我从Rest服务下载类别和帖子,以便将它们存储在SQLite数据库中。我有几个问题: 如何确定哪个对象是类别和帖子?或者我怎么才能访问它们对象变量非常奇怪 我应该把在数据库中插入项目的代码放在哪里 使用房间图书馆 我需要为每个帖子下载图片,我应该在哪里下载 代码: ItemsApi client=this.getClient();//改造2 列表1.\u您是否尝试添加改装的Converter工厂 Retrofit restAdapter = new Retr
ItemsApi client=this.getClient();//改造2
列表1.\u您是否尝试添加改装的Converter工厂
Retrofit restAdapter = new Retrofit.Builder().baseUrl("https://abc")
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RestAuthenticationService restAuthenticationService = restAdapter.create(RestAuthenticationService.class);
在RestAuthenticationService.class中:
公共接口重新验证服务{
@POST(“安全/登录”)
可观察的登录(@Body LoginRequest LoginRequest);
}
你是说在本地处理缓存数据?我认为您应该使用而不是Room/native SQLite
你应该使用或
以下是答案:
1)对于并行请求,应该使用Observable.zip,如下所示
Observable<Boolean> obs = Observable.zip(
client.getCategories(),
client.getPosts(),
(categoriesList, postsList) -> {
// you have here both categories and lists
// write any code you like, for example inserting to db
return true;
});
Observable obs=Observable.zip(
client.getCategories(),
client.getPosts(),
(分类列表,postsList)->{
//这里有类别和列表
//编写您喜欢的任何代码,例如插入db
返回true;
});
这里有参数(categoriesList、postsList)及其类型、列表和列表
2)您应该将代码放在我在注释中指定的位置。确保你有正确的线程
3)也可以在那里下载图像。您可以在函数中使用另一个zip
-s,将并行下载图像、插入数据库等组合在一起。所有这些都应该是可观察的,与zip组合在一起
在zip
中,您可以根据需要组合任意多个观测值,它们的结果将作为组合函数的参数提供。1objects变量非常奇怪
,因为您没有为响应使用正确的POJO类(或者将其转换为您想要的POJO类)。2.示例JSON响应在哪里?3.您不必下载图像,只需使用图像加载库即可。@Shashanth我在文章中添加了更多代码。1.我必须在apply
方法中使用POJO吗?2.这很重要吗?服务返回对象。3.我需要下载它们我已经更新了帖子并添加了更多代码。1.看我的例子。2.我需要在下载内容后进行全文搜索,所以SQLite是我的选择。3.我应该把下载的代码放在哪里?在应用中
?或者在接受
?我认为应该改变使用RxJava进行改装的方式,看看这里:
Retrofit restAdapter = new Retrofit.Builder().baseUrl("https://abc")
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RestAuthenticationService restAuthenticationService = restAdapter.create(RestAuthenticationService.class);
public interface RestAuthenticationService {
@POST("security/login")
Observable<User> login(@Body LoginRequest loginRequest);
}
Observable<Boolean> obs = Observable.zip(
client.getCategories(),
client.getPosts(),
(categoriesList, postsList) -> {
// you have here both categories and lists
// write any code you like, for example inserting to db
return true;
});