【问题标题】:Android RxJava/Gson/Retrofit return some fields as null, when they are notAndroid RxJava/Gson/Retrofit 将某些字段返回为 null,但它们不是
【发布时间】:2017-02-07 15:40:35
【问题描述】:

我已经学习 Android 3 周了,但我遇到了我一整天都未能克服的难题。我用 Dagger 2、Retrofit 和 RxJava 设置了 mvp 项目。一切都按预期工作,但是当我发出 get 请求时,我得到的 observable 有 2 个字段为空。

这是我请求的 json(无法发布链接)gist.githubusercontent.com/TonyNikolov/89720ae547e479fb9e92c81ef4b33bef/raw/dcabde67e7fe9032cff702cc24f0cfe208b7402e/superheroes.json

但可观察到的响应是这个 http:// imgur.com/a/oHxFV

如您所见,“imageUrl”和“secretIdentity”字段为空,但“name”和“id”是正确的。这适用于每个对象。

点击here for full github project

这就是我的 NetModule 的样子

@Module
public class NetModule {
String mBaseUrl;

public NetModule(String mBaseUrl) {
    this.mBaseUrl = mBaseUrl;
}

@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
    return PreferenceManager.getDefaultSharedPreferences(application);
}

@Provides
@Singleton
Cache provideHttpCache(Application application) {
    int cacheSize = 10 * 1024 * 1024;
    Cache cache = new Cache(application.getCacheDir(), cacheSize);
    return cache;
}

@Provides
@Singleton
Gson provideGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return gsonBuilder.create();
}

@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache) {
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.cache(cache);
    return client.build();
}

@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl(mBaseUrl)
            .client(okHttpClient)
            .build();
    return retrofit;
}

}

这是 SuperheroesListPresenter

public class SuperheroesListPresenter implements    PresenterContract.SuperheroesListPresenter {

Retrofit retrofit;
ViewContract.SuperheroesList mView;

@Inject
SuperheroesListPresenter(Retrofit retrofit, ViewContract.SuperheroesList mView) {
    this.retrofit = retrofit;
    this.mView = mView;
}


@Override
public void loadSuperheroes() {
    retrofit.create(superheroService.class).getSuperheroesList().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .unsubscribeOn(Schedulers.io())
            .subscribe(new Observer<List<Superhero>>() {

                @Override
                public void onCompleted() {
                    mView.showComplete();
                }

                @Override
                public void onError(Throwable e) {
                    mView.showError(e.getMessage());
                }

                @Override
                public void onNext(List<Superhero> posts) {
                    mView.showSuperheroes(posts);
                }
            });
}


private interface superheroService {
    @GET("/TonyNikolov/89720ae547e479fb9e92c81ef4b33bef/raw/dcabde67e7fe9032cff702cc24f0cfe208b7402e/superheroes.json")
    Observable<List<Superhero>> getSuperheroesList();
}
}

这是超级英雄课

public class Superhero {

public String name;
public String imageUrl;
public String secretIdentity;
public int id;

Superhero(String name, String imageUrl, String secretIdentity, int id) {
    this.name = name;
    this.secretIdentity = secretIdentity;
    this.imageUrl = imageUrl;
    this.id = id;

}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getSecretIdentity() {
    return this.secretIdentity;
}

public void setSecretIdentity(String secretIdentity) {
    this.secretIdentity = secretIdentity;
}

public String getImageUrl() {
    return this.imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}
}

我知道我应该在构造函数中使用 setter,但是我已经花了 8 个小时来尝试处理这个问题,并且我更改了很多代码来试图找到问题。

【问题讨论】:

  • 自己修复了...问题在于 FieldNamingPolicy 将其更改为 Identity
  • 如果您自己解决了问题,请发布一个简短的答案并接受它
  • GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); 因为字段命名策略不应该说它是 secret_identity 而不是 secretIdentity

标签: android json rx-java retrofit2 dagger-2


【解决方案1】:

在我的情况下,我有一些名为 imageURL 之类的字段并发现

gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);

解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2016-04-27
    • 2021-12-29
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多