0

I am using an API to retrieve info from a database.

I am able to retrieve one object but what I really want to do is make a search which could retrieve 0, 1 or more objects.

To retrieve one object, I have declared a class that is a POJO with the attributes as such:

public class Cosmetic implements Serializable {
    @SerializedName("_id")
    @Expose
    private String id;
    @SerializedName("code")
    @Expose
    private String code;
         ---- omitted ----

And then I use retrofit and RxJava to make the API calls.

final Cosmetic[] cosmetic_result = new Cosmetic[1];
retrofit = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();

service = retrofit.create(CosmeticsService.class);

// Calling '/prod/beauty/{field}'
Single<Cosmetic> callSync = service.getByName(query);

CompositeDisposable compositeDisposable = new CompositeDisposable();
callSync.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        //https://www.baeldung.com/rxjava-error-handling
        .onErrorReturn(Throwable -> productNotFound)
        .subscribe(new SingleObserver<Cosmetic>() {
            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
            }

            @Override
            public void onSuccess(Cosmetic cosmetic) {
                System.out.println("API has responded ");
                result[0] = cosmetic.getCode();
                cosmetic_result[0]=cosmetic;
            }
            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }
        });
// While loop necessary to force waiting for the API result
while(result[0]==""){} // DON'T DELETE

return cosmetic_result[0];

So this works for one JSON object as such:

{
   "_id":"6353e8fe5d63726919402cec",
   "code":"0000016615656"
        --- omitted ---
}

The problem is that by searching the database I can get multiple results which come in a JSON objects like this one:

[
   {
      "_id":"6353e8fe5d63726919402cec",
      "code":"0000016615656",
           -- omitted --
   },
   {
      "_id":"6353e8fe5d63726919402cec",
      "code":"0000016615656",
           -- omitted --
   }
]

How do I parse this new object which is essentially and array of Cosmetics?

I have tried using an ArrayOfCosmetics POJO as suggested here:

public class ArrayOfCosmetics {
    @SerializedName("cosmetic")
    @Expose
    private ArrayList<Cosmetic> cosmetics;
    public ArrayList<Cosmetic> getCosmetics() {
        return cosmetics;
    }
}

but then I get an error at:

.subscribe(new SingleObserver<ArrayOfObjects>() {

I also tried and also got an error at:

.subscribe(new SingleObserver<ArrayList<Cosmetic>>() {
  • Please, provide definition of CosmeticsService.class – artild Nov 18 '22 at 20:08
  • @artild public interface CosmeticsService{ @GET("/prod/beauty/{name_field}") public Single getByName(@Path("name_field") String name_field); – sempre_em_pe Nov 18 '22 at 20:48
  • 1
    so as I see , you should have other method in CosmeticsService (and in corresponding API) which will return collection of Cosmetic. – artild Nov 18 '22 at 20:54

0 Answers0