改造中的一般响应

时间:2016-12-01 15:47:44

标签: retrofit2 generic-programming

我有两次调用使用改造,因为API设计很差我得到了JSON的不同结构,所以我有两个不同的模型对象。

我想要实现的是更小的获取方法并避免重复的代码。我正在尝试使用泛型类来进行pojo但没有成功。

谢谢,

我的模特:

   public class GenreResponse {
      @SerializedName("genre") @Expose private Genre genre;
      @SerializedName("sort") @Expose private String sort;
      @SerializedName("page") @Expose private Integer page;
      @SerializedName("limit") @Expose private Integer limit;
      @SerializedName("events") @Expose private List<Movie> events;
      @SerializedName("subGenres") @Expose private List<SubGenre> subGenres = new ArrayList<>();

      public List<SubGenre> getSubGenres() {
        return subGenres;
      }

      public Genre getGenre() {
        return genre;
      }

      public String getSort() {
        return sort;
      }

      public Integer getPage() {
        return page;
      }

      public Integer getLimit() {
        return limit;
      }

      public List<Movie> getEvents() {
        return events;
      }

    }



    public class GeneralSearchResponse {
      @SerializedName("query")   @Expose private String query;
      @SerializedName("sort")    @Expose private String sort;
      @SerializedName("page")    @Expose private String page;
      @SerializedName("limit")   @Expose private Integer limit;
      @SerializedName("amount")  @Expose private Integer amount;
      @SerializedName("events")  @Expose private List<Movie> events;
      @SerializedName("sideNav") @Expose private List<SideNav> sideNavList = new ArrayList<>();

      public List<Movie> getEvents() {
        return events;
      }

      private class SideNav {
        @SerializedName("id")           @Expose private Integer id;
        @SerializedName("name")         @Expose private String name;
        @SerializedName("englishName")  @Expose private String englishName;
        @SerializedName("amount")       @Expose private Integer amount;
        @SerializedName("isKidsEvents") @Expose private Boolean isKidsEvent;
      }
    }

我的方法:

 private void fetchGenresData(String genreId, int page, final boolean addMore, String sortType) {
    switch (fragmentMode) {
      case MODE_GENRE:
        Observable<GenreResponse> observableGenre = RxUtils.wrapRetrofitCall(
            VODApplication.getInstance()
                .getApi()
                .getGenres(genreType, genreId, page, DEFAULT_PAGE_LIMIT, sortType));

        RxUtils.wrapAsync(observableGenre).subscribe(new Action1<GenreResponse>() {
          @Override public void call(GenreResponse response) {


            if (addMore) {
              mAdapter.remove(mAdapter.getItemCount() - 1);
              mAdapter.addAll(response.getEvents());
            } else {
              // called for the first time.
              mAdapter.setGenres(response.getEvents());
              mSpinnerView.setListener(GenreFragment.this);
              mSpinnerView.initSpinners(response);
            }

            isLoading = false;
          }
        }, new Action1<Throwable>() {
          @Override public void call(Throwable t) {
            ((GenreScreen) getActivity()).getDialog().dismiss();
          }
        });
        break;

      case MODE_SEARCH:
        AppUtils.hideKeyboard(getActivity());


            .enqueue(new Callback<GeneralSearchResponse>() {
              @Override public void onResponse(Call<GeneralSearchResponse> call,
                  Response<GeneralSearchResponse> response) {

                if (addMore) {
                  mAdapter.remove(mAdapter.getItemCount() - 1);
                  mAdapter.addAll(response.body().getEvents());
                } else {
                  // called for the first time.
                  mAdapter.setGenres(response.body().getEvents());
                  mSpinnerView.setListener(GenreFragment.this);
                }

                isLoading = false;
              }

              @Override public void onFailure(Call<GeneralSearchResponse> call, Throwable t) {
                Log.d(TAG, "call: " + t.getMessage());
              }
            });
        break;
    }
  }

0 个答案:

没有答案