我正在使用一个库来轻松从newsapi.org获取一些新闻
implementation 'com.github.KwabenBerko:News-API-Java:1.0.0'
图书馆工作了一次,实际上我是第一次加载新闻……从那以后再也没有。这不是互联网问题,因为我的应用程序可以在线运行,其他一切都可以正常运行。所以我的问题是我怎么能找到问题所在?在logcat中,我收到了用于recycleview适配器的消息,导致其为空(没有数据分发),因此此消息无济于事。 我正在使用的代码:
private void setupNews() {
if(newsArticles.size() != 50) {
NewsApiClient newsApiClient = new NewsApiClient(getString(R.string.feednews_api_key));
newsApiClient.getEverything(
new EverythingRequest.Builder()
.q("Fitness")
.pageSize(50)
.language("en")
.build(),
new NewsApiClient.ArticlesResponseCallback() {
@Override
public void onSuccess(ArticleResponse articleResponse) {
for (int i = 0; i < 50; i++) {
newsArticles.add(new NewsArticle(articleResponse.getArticles().get(i).getTitle(),
articleResponse.getArticles().get(i).getDescription(),
articleResponse.getArticles().get(i).getPublishedAt(),
articleResponse.getArticles().get(i).getSource().getName(),
articleResponse.getArticles().get(i).getUrlToImage(),
articleResponse.getArticles().get(i).getUrl()));
}
RecyclerView newsRecycler = findViewById(R.id.news_articles_list);
Log.d(TAG, "setup: " + newsArticles.size());
ViewCompat.setNestedScrollingEnabled(newsRecycler, false);
newsRecycler.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
RecyclerView.Adapter adapter = new NewsAdapter(newsArticles, getApplicationContext());
newsRecycler.setLayoutManager(layoutManager);
newsRecycler.setAdapter(adapter);
mProgressBar.setVisibility(View.GONE);
}
@Override
public void onFailure(Throwable throwable) {
Log.d(TAG, "onFailure: " + throwable);
System.out.println(throwable.getMessage());
mProgressBar.setVisibility(View.GONE);
mCheckNewsRecieved.setVisibility(View.VISIBLE);
}
}
);
我检查了2个不同的钥匙,因此大概没有钥匙问题。我检查了Recycleview适配器和Viewholder中的所有内容。我阅读了所有在logcat中遇到的E/RecyclerView: No adapter attached; skipping layout
错误的帖子,因此我几乎确信我没有从newsapi.org获取数据。因此,当我思路ideas尽时,我的问题是如何调试此类问题?
更新:我发布了recycleview适配器,以防万一
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private final ArrayList<NewsArticle> mNewsArticle;
private final Context mContext;
public NewsAdapter(ArrayList<NewsArticle> mNewsArticle, Context mContext) {
this.mNewsArticle = mNewsArticle;
this.mContext = mContext;
}
@Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_articles, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(NewsAdapter.ViewHolder holder, int position) {
holder.articleDesc.setText(mNewsArticle.get(position).getArticleDesc());
holder.articleName.setText(mNewsArticle.get(position).getArticleName());
holder.articleURL.setText(mNewsArticle.get(position).getArticleURLName());
holder.articleDate.setText(dateConfiguration(mNewsArticle.get(position).getArticleDate()));
Log.d(TAG, "onBindViewHolder: " + mNewsArticle.get(position).getArticleURLName());
Picasso
.with(mContext)
.load(mNewsArticle.get(position).getArticleImageURL())
.resize(380, 200)
.into(holder.articleImage);
holder.itemView.setOnClickListener(v -> IntentUtils.invokeToURL(mContext, mNewsArticle.get(position).getArticleURLLink()));
}
@Override
public int getItemCount() {
return mNewsArticle.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView articleName;
private TextView articleDesc;
private TextView articleURL;
private ImageView articleImage;
private TextView articleDate;
public ViewHolder(View itemView) {
super(itemView);
articleName = itemView.findViewById(R.id.article_title);
articleDesc = itemView.findViewById(R.id.article_desc);
articleImage = itemView.findViewById(R.id.article_image);
articleURL = itemView.findViewById(R.id.article_url);
articleDate = itemView.findViewById(R.id.article_date);
}
}
public String dateConfiguration(String Date) {
return Date.replace("T", " ").replace("Z", "");
}
更新:检查了newsapi.org之后,它收到了我的请求。奇怪的是,在模拟器中曾经工作过一次,但是现在即使我擦除数据,使缓存无效等,文章仍在加载(旧的)。在物理设备中,我收到了mCheckNewsRecieved.setVisibility(View.VISIBLE);
消息,就像失败了。