简体   繁体   中英

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

Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

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 . Either of below should solve your problem

List<Team> teams;

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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