gpt4 book ai didi

java - 改造 2 - RxJava : Unable to invoke no-args constructor for retrofit2. 调用

转载 作者:行者123 更新时间:2023-12-02 01:34:14 24 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,目标是显示可以使用 PokeApi 在 https://pokeapi.co/ 找到的 Pokemon 列表。

我有两个实例,一个可以在没有 RxJava2 的情况下工作,另一个则不能与 RxJava 2 一起工作。对于这两个实例,我都使用 Retrofit 2。

当我包含 RxJava2 时它不起作用,我收到的错误是

D/thrown: java.lang.RuntimeException: Unable to invoke no-args constructor for retrofit2.Call<za.co.lbnkosi.discoveryassesment.domain.model.RemoteDataObjectModel>. Registering an InstanceCreator with Gson for this type may fix this problem.

此时,我已经浏览了很多与此类似的 Stackoverflow 问题,其中大多数(如果不是全部)都提到了反序列化,但对我来说到目前为止还没有起作用。

我想知道问题是什么或者我做错了什么以及如何解决这个问题。下面我已经包含了相关代码

public interface PokeApi {

//Ignore
@GET("pokemon")
Call<RemoteDataObjectModel> getPokemonList(@Query("limit") int limit, @Query("offset") int offset);

@GET("pokemon")
Observable<Call<RemoteDataObjectModel>> getPokemonList2(@Query("limit") int limit, @Query("offset") int offset);

}
public class RemoteDataObjectModel {

@SerializedName("results")
private ArrayList<RemoteDataModel> results;

public ArrayList<RemoteDataModel> getResults() {
return results;
}

public void setResults(ArrayList<RemoteDataModel> results) {
this.results = results;
}
}
public class RemoteDataModel {

@SerializedName("number")
private int number;

@SerializedName("name")
private String name;

@SerializedName("url")
private String url;

public int getNumber() {
String[] urlItems = url.split("/");
return Integer.parseInt(urlItems[urlItems.length -1]);
}

public void setNumber(int number) {
this.number = number;
}

public String getName() {
return name;
}

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

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
public class RetrofitComponent {

private static RetrofitComponent INSTANCE;
private PokeApi pokeApi;


private RetrofitComponent(){

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();


Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://pokeapi.co/api/v2/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();

pokeApi = retrofit.create(PokeApi.class);

}


public static RetrofitComponent getInstance() {
if (INSTANCE == null) {
INSTANCE = new RetrofitComponent();
}
return INSTANCE;
}

public Observable<Call<RemoteDataObjectModel>> getPokemonList(int limit, int offest) {
return pokeApi.getPokemonList2(30,0);
}


}
private void getPokemonList(PokeApiDataSource.PokemonListCallback callback) {
RetrofitComponent.getInstance()
.getPokemonList(100,0)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Call<RemoteDataObjectModel>>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(Call<RemoteDataObjectModel> remoteDataObjectModelCall) {
Log.d("","");
remoteDataObjectModelCall.enqueue(new Callback<RemoteDataObjectModel>() {
@Override
public void onResponse(@NotNull Call<RemoteDataObjectModel> call, @NotNull Response<RemoteDataObjectModel> response) {
loading = true;
RemoteDataObjectModel pokeApiObjects = response.body();
_arrayList = Objects.requireNonNull(pokeApiObjects).getResults();
callback.pokemonListSuccess();
}
@Override
public void onFailure(@NotNull Call<RemoteDataObjectModel> call, @NotNull Throwable t) {
loading = true;
Log.e(TAG, " onFailure: " + t.getMessage());
}
});
}

@Override
public void onError(Throwable e) {
Log.d("thrown", e.toString());
}

@Override
public void onComplete() {

}
});
}

最佳答案

我认为Call是来自Retrofit的类。它提供了一个回调函数来异步获取响应。但由于您要使用 RxJava,所以 Rxjava 的本质已经是异步的。您可能不需要获取 Call 形式的响应。相反,请尝试这个

public interface PokeApi {

// If you need to get the response body + headers ...
@GET("pokemon")
Observable<Response<RemoteDataObjectModel>> getPokemonList2(@Query("limit") int limit, @Query("offset") int offset);


// If you only need body
@GET("pokemon")
Observable<RemoteDataObjectModel> getPokemonList2(@Query("limit") int limit, @Query("offset") int offset);

// Or the better way, the result from API is only return once. So, Single is more suitable in this case
@GET("pokemon")
Single<RemoteDataObjectModel> getPokemonList2(@Query("limit") int limit, @Query("offset") int offset);

}

关于java - 改造 2 - RxJava : Unable to invoke no-args constructor for retrofit2. 调用 <RemoteDataObjectModel>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55484491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com