Retrofit 2 在生产中返回 null

Ole*_*leg 3 android retrofit rx-java2

我正在尝试解决一个问题,即 Retrofit 2 返回 null 作为响应。有趣的是,在模拟器中一切正常,数据被接收并显示,但是当我构建 APK 时,同样的事情因 NullPointerException 而崩溃。我已经评论了发生这种情况的行。

我的 build.gradle 依赖项:

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'io.reactivex.rxjava2:rxjava:2.1.3'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
Run Code Online (Sandbox Code Playgroud)

初始化改造:

retrofit = new Retrofit.Builder()
        .baseUrl(<url>)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();
Run Code Online (Sandbox Code Playgroud)

界面:

@GET(<url>)
Flowable<DataResponse> getDataFromApi(@Query("offset") int offset, @Query("limit") int limit);
Run Code Online (Sandbox Code Playgroud)

API调用:

retrofit
  .getDataFromApi(0, 10)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Subscriber<DataResponse>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(DataResponse dataResponse) {
          // HERE I GET NULL IN PRODUCTION   
        }
Run Code Online (Sandbox Code Playgroud)

数据响应POJO:

public class ServicesResponse {
  private List<Item> items;

  public List<Item> getItems() {
    return items;
  }
}
Run Code Online (Sandbox Code Playgroud)

前卫。我已经尝试了几件事,但仍然没有运气。它目前看起来像这样:

-dontwarn retrofit2.**
-keep class javax.annotation.Nullable
-keep interface javax.annotation.Nullable
-keep class retrofit2.** { *; }
-keep class com.squareup.okhttp3.** { *; }
-keep interface com.squareup.okhttp3.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
  @retrofit2.http.* <methods>;
}
-keepclasseswithmembers interface * {
  @retrofit2.http.* <methods>;
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助 :)

Jem*_*rov 6

我总是避免混淆 Retrofit 使用的模型类。否则 AFAIK Gson 无法序列化/反序列化模型:

-keep public class your.package.to.models.** {*;} 
Run Code Online (Sandbox Code Playgroud)