Guest User

Untitled

a guest
Sep 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. public class MainFragment extends Fragment
  2. {
  3. protected RecyclerView recyclerView;
  4. protected NewsAdapter adapter;
  5. protected String API_KEY;
  6. String sourceTitle, sourceID;
  7. List<Articles> articleList;
  8.  
  9. public MainFragment() {
  10.  
  11. }
  12.  
  13. @Nullable
  14. @Override
  15. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
  16. {
  17. ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_main, null);
  18.  
  19. sourceTitle = "ABC News";
  20. sourceID = "abc-news";
  21.  
  22. getActivity().setTitle(sourceTitle);
  23.  
  24. API_KEY = getString(R.string.API_KEY);
  25.  
  26. recyclerView = root.findViewById(R.id.recyclerview);
  27.  
  28. RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
  29. recyclerView.setLayoutManager(mLayoutManager);
  30. recyclerView.setItemAnimator(new DefaultItemAnimator());
  31.  
  32. ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
  33.  
  34. Call<News> call = apiService.getArticles(sourceID, API_KEY);
  35.  
  36. call.enqueue(new Callback<News>()
  37. {
  38. @Override
  39. public void onResponse(Call<News> call, Response<News> response)
  40. {
  41.  
  42. //This is where the error occurs
  43. articleList = response.body().getArticles();
  44. }
  45.  
  46. @Override
  47. public void onFailure(Call<News> call, Throwable t)
  48. {
  49. Toast.makeText(getActivity(), "Error in API Call", Toast.LENGTH_SHORT).show();
  50. }
  51. });
  52.  
  53. if(articleList.isEmpty() || articleList.size() == 0)
  54. {
  55. recyclerView.setAdapter(null);
  56. Toast.makeText(getActivity(), "Error in List", Toast.LENGTH_SHORT).show();
  57. }
  58. else
  59. {
  60. adapter = new NewsAdapter(articleList, getActivity());
  61. recyclerView.setAdapter(adapter);
  62. }
  63.  
  64.  
  65. recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), recyclerView, new ClickListener() {
  66. @Override
  67. public void onClick(View view, int position)
  68. {
  69. //onclick code
  70. }
  71.  
  72. @Override
  73. public void onLongClick(View view, int position) {
  74. }
  75. }));
  76. return root;
  77. }
  78.  
  79. public interface ApiInterface
  80. {
  81. @GET("top-headlines")
  82. Call<List<News>> getArticles(@Query("sources") String source, @Query("apiKey") String apiKey);
  83. }
  84.  
  85. public class ApiClient
  86. {
  87. public static final String BASE_URL = "https://newsapi.org/v2/";
  88. private static Retrofit retrofit = null;
  89.  
  90.  
  91. public static Retrofit getClient()
  92. {
  93. if (retrofit==null)
  94. {
  95. retrofit = new Retrofit.Builder()
  96. .baseUrl(BASE_URL)
  97. .addConverterFactory(GsonConverterFactory.create())
  98. .build();
  99. }
  100. return retrofit;
  101. }
  102. }
Add Comment
Please, Sign In to add comment