如何将JSON数据存储到Realm中并将其显示到recyclerview中

在我的应用程序中,我已经实现了一个回收站。 我有一个包含一个机构的用户信息的JSON文件的api。 现在最初我想将数据存储到Realm DB中,然后用这些数据填充我的recyclerview。 因此,我正在试图得到这个程序的一些工作文件。 但是我还没有find明确的解释。 我不确定它的正确程序。 我是否需要使用Httpurlconnection获取所有数据,然后将其存储到Realm中。 或者有任何改变的方式来做到这一点。

编辑问题

得到一些sugesstion后,我已经尝试了follwing code.I使用Retrofit 1.9。 因为我无法使用Retrofit 2+版本获取数据。 正如我从改造1.9得到的结果。 我想保持我的代码基于此。 但是现在对于如何根据领域来设置适配器有困惑。 因为我有json api和realm的独立模型类。 我想先将数据存储到领域,然后将其填充到recaclerview。 在这个时候recyclerview直接与服务器数据填充。

从答案得到指示后,我编辑了我的问题如下

Json回应

[ { "_id": "fhfh49879787989", "dn": "CN=9879798789", "whenChanged": "20170704065349.0Z", "name": "Student", "mail": "student@mail.com", "updated_at": "2017-07-04T18:22:43.624Z" }, { "_id": "595bdcf32c67a3f9ee6c2a25", "dn": "CN=dsfdsfsdfsf", "givenName": "Accounting Office", "whenChanged": "20170801114732.0Z", "name": "Accounting", "mail": "accounting@mail.com", "updated_at": "2017-07-04T18:22:43.641Z" }, { "_id": "584ab3b4122d13e1b0d1578d", "dn": "CN=sfdfsfsdfl", "sn": "Abels", "title": "Student", "givenName": "Gardrut", "whenChanged": "20170807150844.0Z", "department": "PMO", "company": "Multi Lmited", "name": "Mike Lizz", "mail": "mail@yahoo.com", "mobile": "+1321646498", "updated_at": "2016-12-09T13:37:56.175Z" }, { "_id": "584ab3b3122d13e1b0d15735", "dn": "CN=xdfsdfsfsdf", "sn": "Acsdff", "title": "Software Engineer", "givenName": "Olin", "whenChanged": "20170810064841.0Z", "department": "Head", "company": "Private limited", "name": "James Oliver", "mail": "mail@gmail.com", "mobile": "+41228", "updated_at": "2016-12-09T13:37:55.813Z" }, .... ] 

我的模型类

  public class ColleagueModel { @Expose private String _id; @Expose private String dn; @Expose private String givenName; @Expose private String whenChanged; @Expose private String name; @Expose private String mail; @Expose private String updatedAt; @Expose private String sn; @Expose private String title; @Expose private String department; @Expose private String company; @Expose private String mobile; public ColleagueModel(){ } //getter and setter } 

我使用的图书馆

 compile 'com.squareup.retrofit:retrofit:1.9.0' compile 'com.squareup.okhttp:okhttp:2.3.0' compile 'com.google.code.gson:gson:2.3' compile 'com.squareup.picasso:picasso:2.5.2' 

ApiService

 public interface ColleagueApiService { @GET("/api/users") void getColleague(Callback<String> flowers); 

RestManagerClass

  public class ColleagueRestManager { public ColleagueApiService mColleagueApi; public ColleagueApiService getColleagueApi() { if (mColleagueApi == null) { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter( String.class, new StringDeserializer() ); mColleagueApi = new RestAdapter.Builder() .setEndpoint( Constants.HTTP.BASE_URL ) .setConverter( new GsonConverter( gson.create() ) ) .build() .create( ColleagueApiService.class ); } return mColleagueApi; } } 

适配器类

 public class MyColleaguesAdapter extends RecyclerView.Adapter<MyColleaguesAdapter.ColleagueHolder>/{ public static String TAG = MyColleaguesAdapter.class.getSimpleName(); private List<ColleagueModel> mColleague; private Context context; public interface ColleagueListListener { } public MyColleaguesAdapter(List<ColleagueModel> colleagues,Context context) { this.context=context; mColleague = colleagues; } public void addColleague(ColleagueModel colleague) { //Log.d(TAG,colleague.name); mColleague.add(colleague); notifyDataSetChanged(); } @Override public ColleagueHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.colleage_row_layout,parent,false); return new ColleagueHolder(view); } @Override public void onBindViewHolder(ColleagueHolder holder, int position) { final ColleagueModel currentColleague = mColleague.get(position); Picasso.with(holder.itemView.getContext()); holder.colleagueName.setText(currentColleague.getName()); holder.companyName.setText(currentColleague.getCompany()); holder.jobTitle.setText(currentColleague.getTitle()); holder.colleaguePicture.setImageResource( R.drawable.profile_image ); Picasso.with(holder.itemView.getContext()).load( Constants.HTTP.PHOTO_URL + currentColleague.getMail()).into(holder.colleaguePicture); holder.cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent i=new Intent(context,DetailMyColleague.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.putExtra("IMAGE_URL",Constants.HTTP.PHOTO_URL + currentColleague.getMail()); i.putExtra("name",currentColleague.getName()); i.putExtra("title",currentColleague.getTitle()); i.putExtra("company",currentColleague.getCompany()); i.putExtra("mobile",currentColleague.getMobile()); i.putExtra("mail",currentColleague.getMail()); i.putExtra("department",currentColleague.getDepartment()); context.startActivity(i); } }); } @Override public int getItemCount() { return mColleague.size(); } public class ColleagueHolder extends RecyclerView.ViewHolder{ public CardView cardView; public ImageView colleaguePicture; public TextView colleagueName; public TextView companyName; public TextView jobTitle; public ColleagueHolder(View itemView) { super(itemView); colleaguePicture= itemView.findViewById(R.id.colleague_picture); colleagueName= itemView.findViewById(R.id.colleague_name); companyName= itemView.findViewById(R.id.company_name); jobTitle= itemView.findViewById(R.id.job_title); cardView= itemView.findViewById(R.id.cardview_user); } } } 

控制器类

  public class Controller { private static final String TAG = Controller.class.getSimpleName(); private UserCallbackListener mListener; private ColleagueRestManager mApiManager; public Controller(UserCallbackListener listener) { mListener = listener; mApiManager = new ColleagueRestManager(); } public void startFetching(){ mApiManager.getColleagueApi().getColleague(new Callback<String>() { @Override public void success(String s, Response response) { Log.d(TAG, "JSON :: " + s); try { JSONArray array = new JSONArray(s); for(int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); ColleagueModel colleague = new ColleagueModel(); colleague.setName( object.optString( "name" ) ); colleague.setCompany(object.optString("company")); colleague.setTitle(object.optString("title")); colleague.setMail( object.optString("mail")); colleague.setMobile(object.optString("mobile")); colleague.setDepartment(object.optString("department")); mListener.onFetchProgress(colleague); } } catch (JSONException e) { mListener.onFetchFailed(); } mListener.onFetchComplete(); } @Override public void failure(RetrofitError error) { Log.d(TAG, "Error :: " + error.getMessage()); mListener.onFetchComplete(); } }); } public interface UserCallbackListener{ void onFetchStart(); void onFetchProgress(ColleagueModel user); void onFetchProgress(List<ColleagueModel> userList); void onFetchComplete(); void onFetchFailed(); } } 

主要活动

 public class MyColleaguesPage extends AppCompatActivity implements Controller.UserCallbackListener/*MyColleaguesAdapter.ColleagueListListener*/ { private RecyclerView recyclerView; private MyColleaguesAdapter adapter; private List<ColleagueModel> mColleagueList = new ArrayList<>(); private Controller mController; private Realm colleagueRealm; private LinearLayoutManager layoutManager; private RealmResults<MyColleagueModel> colleagueResult; private List<MyColleagueModel> filteredModelList; private RealmChangeListener realmListener; private static final String DIALOG_TAG = "EmployeeDialog"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mycolleagues_layout); configViews(); mController = new Controller(MyColleaguesPage.this); mController.startFetching(); colleagueRealm = Realm.getDefaultInstance(); } private void configViews() { recyclerView = this.findViewById(R.id.colleagues_recycler); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(MyColleaguesPage.this)); recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool()); adapter = new MyColleaguesAdapter(mColleagueList,getApplicationContext()); recyclerView.setAdapter(adapter); } 

}

RealmApplication类

 public class Application extends Application { @Override public void onCreate() { super.onCreate(); Realm.init(this); RealmConfiguration realmConfiguration = new RealmConfiguration.Builder() .deleteRealmIfMigrationNeeded() .build(); Realm.setDefaultConfiguration(realmConfiguration); } 

领域模型

 public class RealmUserModel extends RealmObject{ public static final String ID = "id"; private String id; private String dn; private String givenName; private String whenChanged; private String name; private String mail; private String updatedAt; private String sn; private String title; private String department; private String company; private String mobile; //getter and setter } 

Solutions Collecting From Web of "如何将JSON数据存储到Realm中并将其显示到recyclerview中"

1.)使用jsonschema2pojo.org为json生成API响应类

 public class EmployeeResponse { @SerializedName("_id") @Expose private String id; @SerializedName("dn") @Expose private String dn; @SerializedName("sn") @Expose private String sn; @SerializedName("title") @Expose private String title; @SerializedName("givenName") @Expose private String givenName; @SerializedName("whenChanged") @Expose private String whenChanged; @SerializedName("department") @Expose private String department; @SerializedName("company") @Expose private String company; @SerializedName("name") @Expose private String name; @SerializedName("mail") @Expose private String mail; @SerializedName("mobile") @Expose private String mobile; @SerializedName("updated_at") @Expose private String updatedAt; .... } 

2.)从http://square.github.io/retrofit/添加Retrofit到你的项目,所以你甚至不会考虑使用HttpUrlConnection

 compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.google.code.gson:gson:2.8.1' 

3.)你定义Retrofit界面

 public interface ApiService { @GET("api/users") Call<List<User>> getUsers(); } 

4.)通过Retrofit创buildApiService实现

 public class CustomApplication extends Application { // add to manifest ofc Retrofit retrofit; ApiService service; @Override public void onCreate() { super.onCreate(); retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .baseUrl("https://app.com/") .build(); service = retrofit.create(ApiService.class); } // get, get... } 

5.)创build你的RealmObject

 public class Employee extends RealmObject { private String id ; private String dn ; private String givenName ; private String whenChanged ; private String name ; private String mail ; private String updatedAt ; // maybe Date? private String sn ; private String title; private String department; private String company; private String mobile; //getter and setter method } 

6.)创build映射api响应类的数据的类

 public class EmployeeMapper { Employee toEmployee(EmployeeResponse response) { // ... return employee; } } 

7.)将员工写入领域

 try(Realm r = Realm.getDefaultInstance()) { r.executeTransaction((realm) -> { realm.insertOrUpdate(employees); }); } 

8.)通过文档https://realm.io/docs/java/latest/#adapters通过RealmRecyclerViewAdapter监听Realm的更&#x6539;

 public class EmployeeAdapter extends RealmRecyclerViewAdapter<...