【问题标题】:Retrofit data persistence改造数据持久化
【发布时间】:2017-03-16 04:46:30
【问题描述】:

如何使用 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 或模型应用程序终止时 log cat 没有任何错误

【问题讨论】:

  • 您能否将要点与代码的 sugarorm 选项联系起来?人们可以查看的项目的最小示例?
  • gist.github.com/HemrajRijal/27851ee3ddd2eec4e80344c372725103 模型要点链接与糖 ORM @Ivan 的集成
  • 您是否尝试过在所有回调行以及 api 创建和调试中单步执行代码中设置断点?正在执行哪些行?哪些不是?什么时候终止?如果发生异常时幸运的话,您将能够在更高级别的处理程序中看到它

标签: android retrofit2 activeandroid sugarorm datapersistance


【解决方案1】:

经过大量研究,我成功地保留了使用 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()));
        }

按照上述方法可以帮助我解决我的问题,我希望这也有助于解决您的问题。如果您在实施解决方案时遇到任何问题,可以在评论中询问我

【讨论】:

  • 这个方法可以和任何db一起使用。
  • 此方法仅在 Realm 数据库中进行了测试。
猜你喜欢
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
相关资源
最近更新 更多