我已经学习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个小时试图处理这个问题,并且我改变了很多代码试图找到问题。
答案 0 :(得分:0)
就我而言,我有一些名为imageURL
之类的字段,发现
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
解决了这个问题。