📅  最后修改于: 2023-12-03 15:13:21.932000             🧑  作者: Mango
Android-RSS阅读器是一款用于Android平台的应用程序,它能够帮助用户订阅、管理和阅读RSS(Really Simple Syndication)源的内容。它提供了用户友好的界面和功能,使用户能够方便地浏览和获取自己感兴趣的资讯。
Android-RSS阅读器使用了经典的MVC(Model-View-Controller)架构模式进行开发。
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArticleAdapter adapter;
private List<Article> articles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
articles = new ArrayList<>();
adapter = new ArticleAdapter(this, articles);
recyclerView.setAdapter(adapter);
// 使用Retrofit库发送HTTP请求获取RSS源数据
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
Call<List<Article>> call = apiService.getArticles();
call.enqueue(new Callback<List<Article>>() {
@Override
public void onResponse(Call<List<Article>> call, Response<List<Article>> response) {
if (response.isSuccessful()) {
List<Article> responseArticles = response.body();
articles.clear();
articles.addAll(responseArticles);
adapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<List<Article>> call, Throwable t) {
Toast.makeText(MainActivity.this, "Failed to load articles", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "Failed to load articles", t);
}
});
}
}
以上是一个简单的MainActivity代码示例,它演示了如何使用Retrofit库发送HTTP请求获取RSS源数据,并将数据显示在RecyclerView中。