簡體   English   中英

改造數據持久性

[英]Retrofit data persistence

如何保留使用Retrofit庫從服務器解析的數據。 這樣,即使沒有互聯網連接,用戶也可以查看它。

    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.post_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<ArrayList<Post>> call = apiService.getAllPosts();
    call.enqueue(new Callback<ArrayList<Post>>() {
        @Override
        public void onResponse(Call<ArrayList<Post>> call, Response<ArrayList<Post>> response) {
            int statusCode = response.code();
            List<Post> posts = response.body();
            recyclerView.setAdapter(new PostListAdapter(posts, R.layout.activity_main, getApplicationContext()));

        }

        @Override
        public void onFailure(Call<ArrayList<Post>> call, Throwable t) {
            Log.e(TAG, t.toString());

        }
    });

我想保存響應,保留並顯示數據到RecyclerView中。 幫助保存使用ORM的方法將不勝感激,我嘗試使用Sugar ORM和ActiveAndroid,但在該方法上我無法成功:(

當我擴展SugarRecord或Model App終止時,日志貓上沒有任何錯誤

經過大量研究,我成功地保留了使用Retrofit解析的響應。 我已使用Realm保留數據以供脫機使用。 我將描述我已實現的方法,這些方法可能會幫助其他人遇到這類問題。

1.在Gradle中添加領域依賴

應用程序級Gradle文件

`apply plugin: 'realm-android'` 

repositories {
    maven { url "https://jitpack.io" }
}

classpath "io.realm:realm-gradle-plugin:1.2.0" --> on project level gradle file

2.擴展RealmObject

public class Post extends RealmObject {
    @SerializedName("userId")
    @Expose
    private Integer userId;

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

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

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

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}

3.創建領域配置(如果不存在)

if (realmConfiguration == null) {
            realmConfiguration = new RealmConfiguration.Builder(this)
                    .build();
        }
        realm = Realm.getInstance(realmConfiguration);

4.保存解析的數據

Call<List<Post>> call = apiService.getAllPosts();
        call.enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                closeProgress();
                if (response.isSuccessful()) {
                    List<Post> posts = response.body();
                    mRecyclerView.setAdapter(new PostAdapter(posts, R.layout.activity_main, getApplicationContext()));
                    for (int i = 0; i < posts.size(); i++) {
                        realm.beginTransaction();
                        Post post = realm.createObject(Post.class);
                        post.setUserId(posts.get(i).getUserId());
                        post.setId(posts.get(i).getId());
                        post.setTitle(posts.get(i).getTitle());
                        post.setBody(posts.get(i).getBody());
                        realm.commitTransaction();
                    }
                    mRecyclerView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {
               t.printStackTrace();
            }
        });

5.查詢和填充Realm保存的數據

RealmResults<Post> posts = realm.where(Post.class).findAll();
            mRecyclerView.setAdapter(new PostAdapter(posts, R.layout.activity_main, getApplicationContext()));
        }

遵循上述方法可以幫助我解決我的問題,希望也能解決您的問題。 如果您在實施解決方案時遇到任何問題,可以在評論中問我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM