Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装2在生产中返回空值_Android_Retrofit_Rx Java2 - Fatal编程技术网

Android 改装2在生产中返回空值

Android 改装2在生产中返回空值,android,retrofit,rx-java2,Android,Retrofit,Rx Java2,我正试图解决一个问题,即改装2的响应返回null。有趣的是,在emulator中,一切都很好,数据都被接收和显示,但当我构建APK时,同样的事情会在NullPointerException中崩溃。我已经评论了发生这种情况的那一行 我的build.gradle依赖项: compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'io.reactivex.rxjava2:rxjava:2.1.3' compile 'io.reactivex.rx

我正试图解决一个问题,即改装2的响应返回null。有趣的是,在emulator中,一切都很好,数据都被接收和显示,但当我构建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'
初始化改装:

retrofit = new Retrofit.Builder()
        .baseUrl(<url>)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();
reformation=new reformation.Builder()
.baseUrl()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
接口:

@GET(<url>)
Flowable<DataResponse> getDataFromApi(@Query("offset") int offset, @Query("limit") int limit);
@GET()
可流动的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   
        }
改装
.getDataFromApi(0,10)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(新订户(){
@凌驾
认购的公共无效(认购){
s、 请求(长最大值);
}
@凌驾
public void onNext(DataResponse DataResponse){
//这里我在生产中得到空值
}
数据响应POJO:

public class ServicesResponse {
  private List<Item> items;

  public List<Item> getItems() {
    return items;
  }
}
公共类服务响应{
私人清单项目;
公共列表getItems(){
退货项目;
}
}
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>;
}
-dontwarn 2**
-保持类javax.annotation.Nullable
-保持接口javax.annotation.Nullable
-保持类2.*{*;}
-保持类com.squareup.okhttp3.*{*;}
-保持接口com.squareup.okhttp3.*{*;}
-保留署名
-保留特例
-keepclassswithmembers类*{
@2.http.*;
}
-KeepClassSwithMembers接口*{
@2.http.*;
}

感谢您的帮助:)

我始终保持改型使用的模型类不受混淆。否则,AFAIK Gson无法序列化/反序列化模型:

-keep public class your.package.to.models.** {*;} 
在POJO中使用@SerializedName(“项”),不要禁用模型的ProGuard

public class ServicesResponse {
  @SerializedName("items")
  private List<Item> items;

  public List<Item> getItems() {
    return items;
  }
}
公共类服务响应{
@序列化名称(“项目”)
私人清单项目;
公共列表getItems(){
退货项目;
}
}

你能发布NPE堆栈跟踪吗?你是否使用ProGuard(这通常是发布版本中行为不同时出现此类问题的原因)?是的,忘记了ProGuard。我已经更新了原始帖子。当我尝试对响应执行任何操作时,堆栈跟踪基本上是nullpointerexception。-将你的.package.to.models保留为公共类。**{*;}@杰姆希蒂斯肯德洛夫:成功了!简直不敢相信错误就在那里……是的,就是这样,非常感谢!我实际上删除了所有其他与改型相关的东西,okhttp3和javax注释,就在左边-dontwarn javax.annotation.*。一切似乎都在工作。成功了!谢谢。这是一个已知的问题吗?我在任何地方都找不到。@InamUlHuq不是这是一个问题,与改型代码无关。这是Proguard的工作方式,它会更改字段/函数/类的名称…您需要知道何时不更改。