Retrofit 2が応答でnullを返す問題を修正しようとしています。面白いのは、エミュレータではすべて正常に動作し、データは受信されて表示されますが、APKをビルドすると、同じことがNullPointerExceptionでクラッシュすることです。私はこれが起こるところの行をコメントしました。Retrofit 2が製造時にnullを返す
マイ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'
初期化改修:
retrofit = new Retrofit.Builder()
.baseUrl(<url>)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
インタフェース:
@GET(<url>)
Flowable<DataResponse> getDataFromApi(@Query("offset") int offset, @Query("limit") int limit);
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
}
DataResponse POJO:
public class ServicesResponse {
private List<Item> items;
public List<Item> getItems() {
return items;
}
}
ProGuardの。私はいくつかのことを試みましたが、まだ運がありません。助けのための
-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>;
}
感謝:)
NPEスタックトレースを投稿できますか? ProGuardを使用していますか(リリースビルドの動作が異なる場合、通常このような問題が発生します)。 –
はい、ProGuardを忘れました。元の投稿を更新しました。スタックトレースは、私がレスポンスで何かをしようとすると、基本的にはnullpointerexceptionです。 – Oleg
-keep public class your.package.to.models。** {*;} –