简体   繁体   English

Android:使用 RxJava 的 Api 调用返回空响应

[英]Android: Api call using RxJava returning null response

I'm using Retrofit to make API call, When I handle the response I get an error, I'm trying to get data from this API call on this page https://my-json-server.typicode.com/jayraic/demo/db我正在使用 Retrofit 进行 API 调用,当我处理响应时出现错误,我试图从这个页面上的这个 API 调用中获取数据https://my-json-server.typicode.com/jayraic/演示/数据库

Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference尝试在空对象引用上调用接口方法“java.lang.Object java.util.List.get(int)”

Retrofit base call改造碱基调用

public class NBADataFactory {

        private final static String BASE_URL = "https://my-json-server.typicode.com/jayraic/demo/";
        public final static String DB_URL = "https://my-json-server.typicode.com/jayraic/demo/db";

        public static NBAService create() {
            Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            return retrofit.create(NBAService.class);

        }

Retrofit Call改造呼叫

private void fetchTeamList() {

        NBAApplication nbaApplication = NBAApplication.create(context);
        NBAService nbaService = nbaApplication.getNbaService();

        Disposable disposable = nbaService.fetchTeam(NBADataFactory.DB_URL)
                .subscribeOn(nbaApplication.subscribeScheduler())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<NBAResponse>() {
                    @Override
                    public void accept(NBAResponse nbaResponse) {
                        changeTeamDataSet(nbaResponse.getTeamList());
                        teamProgress.set(View.GONE);
                        teamLabel.set(View.GONE);
                        teamRecycler.set(View.VISIBLE);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        messageLabel.set(context.getString(R.string.error_loading_people));
                        teamProgress.set(View.GONE);
                        teamLabel.set(View.VISIBLE);
                        teamRecycler.set(View.GONE);
                    }
                });

        compositeDisposable.add(disposable);
    }

    private void changeTeamDataSet(List<Team> teams) {
        teamList.addAll(teams);
        setChanged();
        notifyObservers();
    }

Response Model响应模型

public class NBAResponse {

    List<Team> teamList;

    public List<Team> getTeamList() {
        return teamList;
    }
}

Team Model团队模式

public class Team implements Serializable {

    @SerializedName("id") public String id;

    @SerializedName("full_name") public String full_name;

    @SerializedName("win") public String win;

    @SerializedName("losses") public String losses;

    @SerializedName("players") public List<Player> players;
}

I'm trying this out and stuck at this issue, any help or direction to right path would be appreciated.我正在尝试这个并坚持这个问题,任何帮助或方向正确的方向将不胜感激。

The response json has teams as outer array name, but in your response Pojo class has its name teamList .响应 json 将teams作为外部数组名称,但在您的响应中 Pojo 类的名称为teamList Either of below should solve your problem以下任何一个都可以解决您的问题

List<Team> teams;

or或者

@SerializedName("teams")
List<Team> teamList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM