Я уже несколько дней пытаюсь получить данные из массива "content", но я получаю нулевой ответ
Мой JSON:
{
"Magazines":[
{
"Title":"someTitle",
"Year":"2019",
"Description":"someDescription",
"Type":"anyType",
"Cover":"http://img.timeinc.net/time/magazine/archive/covers/1956/1101560514_400.jpg",
"Info":"infoAboutMagazine",
"Content":[
{
"ArticleTitle":"someArticleTitle",
"ArticlePrice":"101$"
},
{
"ArticleTitle":"1.2)someArticleTitle",
"ArticlePrice":"102$"
},
{
"ArticleTitle":"1.3)someArticleTitle",
"ArticlePrice":"103$"
}
]
},
{
"Title":"someTitle2",
...
"Content":[{
...
}]
}
]
}
Мой POJO
-----------------------------------Content.java-----------------------------------
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Content {
@SerializedName("ArticleTitle")
@Expose
private String articleTitle;
@SerializedName("ArticlePrice")
@Expose
private String articlePrice;
public String getArticleTitle() {
return articleTitle;
}
public void setArticleTitle(String articleTitle) {
this.articleTitle = articleTitle;
}
public String getArticlePrice() {
return articlePrice;
}
public void setArticlePrice(String articlePrice) {
this.articlePrice = articlePrice;
}
}
-----------------------------------Magazines.java-----------------------------------
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Magazines {
@SerializedName("Magazines")
@Expose
private List<Magazine> magazines = null;
public List<Magazine> getMagazines() {
return magazines;
}
public void setMagazines(List<Magazine> magazines) {
this.magazines = magazines;
}
}
-----------------------------------Magazine.java-----------------------------------
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Magazine {
@SerializedName("Title")
@Expose
private String title;
@SerializedName("Year")
@Expose
private String year;
@SerializedName("Description")
@Expose
private String description;
@SerializedName("Type")
@Expose
private String type;
@SerializedName("Cover")
@Expose
private String cover;
@SerializedName("Info")
@Expose
private String info;
@SerializedName("Content")
@Expose
private List<Content> content = null;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public List<Content> getContent() {
return content;
}
public void setContent(List<Content> content) {
this.content = content;
}
}
Моя активность
public class LastMagazineContentActivity extends AppCompatActivity implements ContentListView {
private RecyclerView rvContent;
private AdapterContent adapter;
private ContentListPresenter presenter;
private Toolbar toolbar;
private List<Content> content;
private static final String TAG = LastMagazineContentActivity.class.getName();
public static LastMagazineContentActivity newInstance() {
return new LastMagazineContentActivity();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_magazine_content);
toolbar = findViewById(R.id.toolbar_content_magazine);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.arrow_left_black);
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
TextView contentTopTitle = findViewById(R.id.contentTitleNumberMagazine);
content = new ArrayList<>();
presenter = new ContentListPresenter(this);
rvContent = (RecyclerView) findViewById(R.id.rv_content);
rvContent.setHasFixedSize(true);
LinearLayoutManager rvContentManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rvContent.setLayoutManager(rvContentManager);
adapter = new AdapterContent();
adapter.setContent(new ArrayList<Content>(), this);
rvContent.setAdapter(adapter);
presenter.loadDateContent();
}
@Override
protected void onDestroy() {
presenter.disposeDisposable();
super.onDestroy();
}
@Override
public void showDataContent(List<Content> content) {
adapter.setContent(content, this);
}
@Override
public void showError() {
Toast.makeText(this, "Ошибка загрузки содержимого", Toast.LENGTH_SHORT).show();
}
}
Мой адаптер
public class AdapterContent extends RecyclerView.Adapter<AdapterContent.AdapterContentViewHolder> {
private List<Content> content;
private Context context;
public List<Content> getContent() {
return content;
}
public void setContent(List<Content> content, Context context) {
this.content = content;
this.context = context;
notifyDataSetChanged();
}
@NonNull
@Override
public AdapterContentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutWithArticle = R.layout.content_item;
LayoutInflater inflater = LayoutInflater.from(context);
View currentView = inflater.inflate(layoutWithArticle, parent, false);
AdapterContentViewHolder adapterContentViewHolder = new AdapterContentViewHolder(currentView);
return adapterContentViewHolder;
}
@Override
public void onBindViewHolder(@NonNull AdapterContentViewHolder holder, int position) {
Content currentDataMagazine = content.get(position);
holder.contentArticleTitleView.setText(currentDataMagazine.getArticleTitle());
holder.article_priceView.setText(currentDataMagazine.getArticlePrice());
}
@Override
public int getItemCount() {
if(content == null) return 0;
return content.size();
}
class AdapterContentViewHolder extends RecyclerView.ViewHolder {
private TextView contentArticleTitleView;
private TextView article_priceView;
public AdapterContentViewHolder(@NonNull View itemView) {
super(itemView);
contentArticleTitleView = itemView.findViewById(R.id.contentArticleTitle);
article_priceView = itemView.findViewById(R.id.article_price);
}
}
}
Мой докладчик
public class ContentListPresenter {
private CompositeDisposable compositeDisposable;
private ContentListView view;
public ContentListPresenter(ContentListView view) {
this.view = view;
}
public void loadDateContent() {
ApiMagazine apiMagazine = ApiMagazine.getInstance();
ApiService apiService = apiMagazine.getApiService();
compositeDisposable = new CompositeDisposable();
Disposable disposable = apiService.getContent()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Magazine>() {
@Override
public void accept(Magazine magazine) throws Exception {
view.showDataContent(magazine.getContent());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
view.showError();
}
});
compositeDisposable.add(disposable);
}
public void disposeDisposable() {
if (compositeDisposable != null) {
compositeDisposable.dispose();
}
}
}
И myPresenterView
public interface ContentListView {
void showDataContent(List<Content> content);
void showError();
}
При переключении на активность необходимые данные не отображаются. После попытки проверить вывод, он возвращает "Attempt to invoke interface method 'int java.util.List.size ()' on a null object reference"
и сбрасывает страницу. После проверки он показал, что здесь: AdapterContent.getItemCount - null
Это мое первое приложение, так что все сложно. Я надеюсь, что кто-нибудь поможет мне.
Добавлено ApiService
public interface ApiService {
@GET("test.json")
Observable<Magazine> getContent();
@GET("test.json")
Observable<Magazines> getMagazines();
}
и ApiMagazine
public class ApiMagazine {
private static ApiMagazine apiMagazine;
private static Retrofit retrofit;
private static final String BASE_URL = "http://test.satird.ru/";
private ApiMagazine() {
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(BASE_URL)
.build();
}
public static ApiMagazine getInstance() {
if (apiMagazine == null) {
apiMagazine = new ApiMagazine();
}
return apiMagazine;
}
public ApiService getApiService() {
return retrofit.create(ApiService.class);
}
}