简体   繁体   中英

Retrofit 2 with Realm response filed

please help me :< If i`m without extend my model with realm it good,but with realm all my items are invisible :<

there are code

My model:

@RealmModule(library = true, allClasses = true)
public class Books extends RealmObject {


    @Expose
    @SerializedName("photo")
    private String photo;

    @Expose
    @SerializedName("rate")
    private Integer rate;

    @Expose
    @SerializedName("rating")
    private Double rating;

My Fragment

restRequest.getAllBooks().enqueue(new Callback<List<Books>>() {
            @Override
            public void onResponse(Response<List<Books>> response, Retrofit retrofit) {
                //завершить наш прогресс диалог
                if (response.isSuccess()) {
                    //Создаем коллекцию
                    List<Books> booksList = response.body();
                    realm = Realm.getDefaultInstance();
                    realm.beginTransaction();
                    List<Books> booking = realm.copyToRealm(booksList);
                    realm.commitTransaction();
                    booksAdapter.Pagination(booking);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

and Retrofit

        Retrofit retrofit = new Retrofit.Builder()

                .baseUrl(BASE_URL)

                .client(new OkHttpClient())

                .addConverterFactory(GsonConverterFactory.create())
                //собрать
                .build();

        INSTANCE.boksAPI = retrofit.create(BoksAPI.class);
    }

Please help,i dont know what it happen with my clases,but realm is work,cuz i see how memory grow up :O

I don't see that you configured Gson correctly. Somethere in your code should be similar initialization:

Gson gson = new GsonBuilder()
    .setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getDeclaringClass().equals(RealmObject.class);
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    })
    .create();

Look at the documentation for more details: https://realm.io/docs/java/latest/#gson

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM