Android 让Retrofit与Realm、Parceler一起使用

来源:互联网 发布:网络安全法解读—试卷 编辑:程序博客网 时间:2024/05/01 22:59

英文原文: Using Retrofit with Realm and Parceler  。

Retrofit是一个绝大多数app都会考虑使用的一个库。如果你的app需要一个后端,那么你就应该使用Retrofit去和RESTful服务交互。它的使用极为简单,你可以瞬间就让网络运行起来,Retrofit自动把获取的响应结果解析到你的model对象中。

通常,你还会考虑把从后端获取的某些数据保存在本地。Realm现在非常流行,用它你可以使用“普通”的对象,而不是使用SQLite(虽然有很多ORM库存在)。而且,RealmObject是live Object,因此你总是能得到最新的数据。

另一个经常出现的问题是你可能只想保存一部分获取的对象到数据库,但是剩下的数据你仍然需要在Activity被杀死的时候使用onSaveInstanceState保存下来(比如一个一个本地缓存的书签或者喜欢item的列表)。Parceler 为这个问题提供了一个简单的解决方法。因为它可以通过注解处理器让你的对象可序列化。

这三个库一起可以让处理后端数据变的简单快速。这篇博客将带你学习设置过程。

Retrofit

本文假设你的后台公开的是一个RESTful服务,响应是JSON格式的。对于JSON的解析,我们将使用Gson。在build.gradle中添加如下依赖:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. compile 'com.google.code.gson:gson:2.6.2'  
  2. compile 'com.squareup.retrofit2:retrofit:2.0.2'  
  3. compile 'com.squareup.retrofit2:converter-gson:2.0.2'  
  4. compile 'com.squareup.okhttp3:okhttp:3.2.0'  
  5. compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'  
第一步就是定义你的model classes,只需创建一个简单的对象。

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public class Country {  
  2.   public String alpha2Code;  
  3.   public String name;  
  4.   public String region;  
  5.   public List<String> languages;  
  6.   ...  
  7. }  
现在我们来定义网络调用(Call)以备用。在Retrofit中,这是通过创建一个接口并为每一个调用定义一个方法来完成的。每一个方法的返回类型是Call<ModelClass>。当然如果call的返回是一个数组,也可以是Call<List<ModelClass>>。每一个call的参数可以使用@Query,@Path 或者 @Body注解,这取决于你的需要。最后,用HTPP请求类型和URL(相对于服务的根地址)来注解这个调用。比如:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public interface ICountryApi {  
  2.   @GET("region/{region}")  
  3.   Call<List<Country>> getAllCountries(@Path("region") String region);  
  4. }  
现在是发挥Retrofit神奇之处的时候了。Retrofit做了所有繁重的工作并创建了一个实现了你的接口的对象。这个对象应该可以被重复使用。比如可以使用依赖注入或者单例来实现重用。

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. Gson gson = new GsonBuilder()  
  2.   .setExclusionStrategies(new ExclusionStrategy() {  
  3.     // This is required to make Gson work with RealmObjects  
  4.     @Override public boolean shouldSkipField(FieldAttributes f) {  
  5.       return f.getDeclaringClass().equals(RealmObject.class);  
  6.     }  
  7.     
  8.     @Override public boolean shouldSkipClass(Class<?> clazz) {  
  9.       return false;  
  10.     }  
  11.   }).create();  
  12.     
  13. OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();  
  14.     
  15. if(BuildConfig.DEBUG) {  
  16.   // enable logging for debug builds  
  17.   HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();  
  18.   loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);  
  19.   httpClientBuilder.addInterceptor(loggingInterceptor);  
  20. }  
  21.     
  22. ICountryApi countryApi = new Retrofit.Builder()  
  23.       .baseUrl("https://restcountries.eu/rest/v1/")  
  24.       .addConverterFactory(GsonConverterFactory.create(gson))  
  25.       .callFactory(httpClientBuilder.build())  
  26.       .build().create(ICountryApi.class);  
现在你就使用service实例做后台调用了。Call可以通过Call.execute()以同步方式执行,它直接返回一个Response<ModelClass>,如果失败抛出异常;或者通过基于回调的Call.enqueue()异步的执行。绝大多数情况下,你想用的都是异步的enqueue()。

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. countryApi.getAllCountries().enqueue(new Callback<List<Country>>() {  
  2.   @Override public void onResponse(Call<List<Country>> call, Response<List<Country>> response) {  
  3.     if(response.isSuccessful()) {  
  4.       List<Country> countries = response.body();  
  5.     } else {  
  6.       // handle error  
  7.     }  
  8.   }  
  9.     
  10.   @Override public void onFailure(Call<List<Country>> call, Throwable t) {  
  11.     // handle error  
  12.   }  
  13. });  

添加对RxJava的支持

后端调用同样可以用RxJava Observable<ModelClass>作为返回类型,这样可以利用Observable带来的好处,比如链式调用。为此,你需要再添加一个依赖并且在创建Retrofit service的时候添加一个call adapter factory:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'  
  2.   
  3. w Retrofit.Builder()  
  4. .addCallAdapterFactory(RxJavaCallAdapterFactory.create())  
  5. ...  

Realm

为了把Realm引入你的app,你需要添加一个buildscript dependency "io.realm:realm-gradle-plugin:0.88.2"并且在app的build.gradle中应用一个plugin:apply plugin: 'realm-Android'。记住Realm会让你的apk增大约3m,因为为了兼容多个CPU架构,Realm要使用的本地libs必须包含进来。不过你可以使用APK Splits来减小这个负担。

RealmObject是通过打开和查询一个Realm获得。一个Realm总是被限制在一个线程中,如果你想在另一个线程中获取RealmObject,必须打开一个新的Realm。但是因为RealmObject是live Object,你总能得到最新的数据,不过如果你的线程不是一个looper线程,你需要手动调用Realm instance的 refresh() 才能看见发生变化变化。你也可以实现interface,添加自定义的setters/getters方法,或者直接使用public 成员。但是对象仍然必须继承RealmObject。你还可以通过注解添加一个primary key或者定义索引。更多信息参见Realm 文档。

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public class Country extends RealmObject {  
  2.   @PrimaryKey  
  3.   public String alpha2Code;  
  4.   public String name;  
  5.   public String region;  
  6.   public RealmList<RealmString> languages;  
  7.   ...  
  8. }  

另一个(烦人)的限制是一个RealmList只能包含RealmObject。因此你需要如下封装基本数据类型和String:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public class RealmString extends RealmObject {  
  2.   public String value;  
  3. }  

但是,现在Retrofit 和 Gson的问题来了。Gson并不知道RealmString是一个封装的对象。。。因此让我们实现一个TypeAdapter来让它正常工作:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public class RealmStringListTypeAdapter extends TypeAdapter<RealmList<RealmString>> {  
  2.   public static final TypeAdapter<RealmList<RealmString>> INSTANCE =   
  3.       new RealmStringListTypeAdapter().nullSafe();  
  4.     
  5.   private RealmStringListTypeAdapter() { }  
  6.     
  7.   @Override public void write(JsonWriter out, RealmList<RealmString> src) throws IOException {  
  8.     out.beginArray();  
  9.     for(RealmString realmString : src) { out.value(realmString.value); }  
  10.     out.endArray();  
  11.   }  
  12.     
  13.   @Override public RealmList<RealmString> read(JsonReader in) throws IOException {  
  14.     RealmList<RealmString> realmStrings = new RealmList<>();  
  15.     in.beginArray();  
  16.     while (in.hasNext()) {  
  17.       if(in.peek() == JsonToken.NULL) {  
  18.         in.nextNull();  
  19.       } else {  
  20.         RealmString realmString = new RealmString();  
  21.         realmString.value = in.nextString();  
  22.         realmStrings.add(realmString);  
  23.       }  
  24.     }  
  25.     in.endArray();  
  26.     return realmStrings;  
  27.   }  
  28. }  

你必须在建立Gson实例的时候注册这个TypeAdapter:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. new GsonBuilder()  
  2.   .registerTypeAdapter(new TypeToken<RealmList<RealmString>>(){}.getType(),  
  3.                         RealmStringListTypeAdapter.INSTANCE)  
  4.   ...  
现在你的RealmObject就能被持久化了。使用Realm的时候,写入操作需要被写在Realm 事务代码之间:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. Realm realm = Realm.getDefaultInstance();  
  2. List<Country> countries = response.body();  
  3. realm.beginTransaction();  
  4. realm.copyToRealmOrUpdate(countries);  
  5. realm.commitTransaction();  
保存了这个对象之后,它们就能通过Realm的query方法取出 - 但是记住 live RealmObject是受线程限制的。

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. List<Country> allSavedCountries = realm.allObjects(Country.class);  
  2. Country specificCountry = realm.where(Country.class).equalTo("alpha2Code""AT").findFirst();  

使用这种方法,你很容易混淆live RealmObject与同一类的detached object。如果你想得到一个detached 拷贝,你可以使用realm.copyFromRealm(liveRealmObject);你可以使用realmObject.isValid()去检查一个RealmObject是一个 live instance还是一个detached 拷贝。 ps :没看懂。

Parceler

在安卓中要传递数据或者保存状态,对象需要实现Serializable或者Parcelable。Parcelable被认为更快,因为它没有反射的负担(以及更少的内存),因此更适合移动app。但是实现一个Parcelable需要做更多的工作。虽然Android Studio有一个自动生成代码的工具,但是每次class改变的时候都要重复这一步。Parceler可以解决这个问题。

因为Parceler使用了一个注解处理器,因此首先需要应用Android APT 插件,那样你的IDE才能知道生成的类,而注解处理产生的代码菜不会包含在apk中。添加'com.neenbedankt.gradle.plugins:android-apt:1.8' buildscript dependency并apply plugin: 'com.neenbedankt.android-apt'。然后你就能在依赖中添加Parceler lib了:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. compile 'org.parceler:parceler-api:1.1.1'  
  2. apt 'org.parceler:parceler:1.1.1'  
有了Parceler,你可以使用@Parcel 来注解class,注解处理器将自动为你创建一个Parcelable的实现。为了让它能和RealmObject工作,你需要一些额外的配置:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. @Parcel(implementations = { CountryRealmProxy.class },  
  2.         value = Parcel.Serialization.FIELD,  
  3.         analyze = { Country.class })  
  4. public class Country extends RealmObject {  
  5.   @PrimaryKey  
  6.   public String alpha2Code;  
  7.   public String name;  
  8.   public String region;  
  9.   @ParcelPropertyConverter(RealmListParcelConverter.class)  
  10.   public RealmList<RealmString> languages;  
  11.   ...  
  12. }  

通过设置analyze与implementations属性,告诉Parcele接受CountryRealmProxy对象-Realm使用的代理class。如果你的RealmObject的RealmProxy类不存在,尝试编译项目,就像这一步里它被生成的那样。

Parceler默认并不知道如何处理一个RealmList。你必须提供一个自定义的ParcelConverter。我写了一个可以和任意RealmList工作的RealmListParcelConverter,只要 item也是用@Parcel注解的。

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public class RealmListParcelConverter  
  2.         implements TypeRangeParcelConverter<RealmList<? extends RealmObject>,   
  3.                                             RealmList<? extends RealmObject>> {  
  4.   private static final int NULL = -1;  
  5.     
  6.   @Override  
  7.   public void toParcel(RealmList<? extends RealmObject> input, Parcel parcel) {  
  8.     parcel.writeInt(input == null ? NULL : input.size());  
  9.     if (input != null) {  
  10.       for (RealmObject item : input) {  
  11.         parcel.writeParcelable(Parcels.wrap(item), 0);  
  12.       }  
  13.     }  
  14.   }  
  15.     
  16.   @Override  
  17.   public RealmList fromParcel(Parcel parcel) {  
  18.     int size = parcel.readInt();  
  19.     RealmList list = new RealmList();  
  20.     for (int i=0; i<size; i++) {  
  21.       Parcelable parcelable = parcel.readParcelable(getClass().getClassLoader());  
  22.       list.add((RealmObject) Parcels.unwrap(parcelable));  
  23.     }  
  24.     return list;  
  25.   }  
  26. }  

要把@Parcel 注解的对象当作Parcelable使用,你必须用Parcels.wrap(object)来封装它们。如果要从一个Parcelable中获得原始的对象,调用Parcels.unwrap(parcelable)。这些方法对@Parcel注解的对象的list同样适用。

结论

设置Retrofit与Realm和Parceler一起使用需要些初始工作,但是使用起来真的很简单:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. List<Country> countries;  
  2.     
  3. public void getCountries() {  
  4.   countryApi.getAllCountries().enqueue(new Callback<List<Country>>() {  
  5.     @Override public void onResponse(Call<List<Country>> call, Response<List<Country>> response) {  
  6.       if(response.isSuccessful()) {  
  7.         countries = response.body();  
  8.         realm.beginTransaction();  
  9.         // Copy the objects to realm. The list still contains detached objects.  
  10.         // If you want to use the live objects, you have to use the return value  
  11.         // of this call.  
  12.         realm.copyToRealmOrUpdate(countries);  
  13.         realm.commitTransaction();  
  14.       } else {  
  15.         // handle error  
  16.       }  
  17.     }  
  18.     
  19.     @Override public void onFailure(Call<List<Country>> call, Throwable t) {  
  20.       // handle error  
  21.     }  
  22.   });  
  23. }  
  24.     
  25. @Override  
  26. protected void onSaveInstanceState(Bundle outState) {  
  27.   super.onSaveInstanceState(outState);  
  28.   outState.putParcelable("countries", Parcels.wrap(countries));  
  29. }  
  30.     
  31. @Override  
  32. protected void onCreate(Bundle savedInstanceState) {  
  33.   ...  
  34.   if(savedInstanceState != null) {  
  35.       countries = Parcels.unwrap(savedInstanceState.getParcelable("countries"));  
  36.   }  
  37. }  

本文相关项目可以在 GitHub上获取

转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0428/4190.html






0 0
原创粉丝点击