I am working on the application, where on the application startup I am downloading categories and posts from Rest service for storing them in the SQLite database. I have a few questions:
- How can I determine which object is Category and which Post? Or how can I access them at all?
objectsvariable is pretty strange. - Where I should put the code for inserting items in the database using Room library?
- I need to download images for each post, where I should do so?
Code:
ItemsApi client = this.getClient(); // Retrofit2
List<Observable<?>> requests = new ArrayList<>();
requests.add(client.getCategories());
requests.add(client.getPosts());
Observable<Object> combined = Observable.zip(
requests,
new Function<Object[], Object>() {
@Override
public Object apply(Object[] objects) throws Exception {
Timber.d("Length %s", objects.length); // Length 2
Timber.d("objects.getClass() %s", objects.getClass()); // objects.getClass() class [Ljava.lang.Object;
return new Object();
}
});
Disposable disposable = combined.subscribe(
new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
Timber.d("Object %s", o.toString());
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable e) throws Exception {
Timber.d("error: %s", e.toString());
}
}
);
private ItemsApi getClient() {
Retrofit.Builder builder = new Retrofit
.Builder()
.client(this.getOkHttpClient())
.addConverterFactory(GsonConverterFactory.create(this.getGson()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.baseUrl(Config.WEBSERVICE_URL_PREFIX);
return builder.build().create(ItemsApi.class);
}
ItemsApi.class:
public interface ItemsApi {
@GET("categories")
Observable<List<CategoryEntity>> getCategories();
@GET("posts")
Observable<List<ArticleEntity>> getPosts();
}
objects variable is pretty strangebecause you're not using proper POJO class for the response (or caste it to POJO class you want). 2. Where's the sample JSON response? 3. You don't have to download images, just use image loading libraries instead. - Shashanthapplymethod? 2. Is it matters? service returns objects. 3. I need to download them - LONGMAN