I. Link RSS từ Google
--XML search: https://news.google.de/news/feeds?pz=1&cf=vi_vn&ned=vi_vn&hl=vi_vn&q=MTP
- Thay "MTP" bằng cụm từ khóa tìm kiếm bất kỳ
- Dùng POSTMAN để kiểm tra API: https://www.postman.com/
- Dạng file RSS thường trông như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0"> <channel> <generator>NFE/5.0</generator> <title>"MTP" - Google Tin tức</title> <link>https://news.google.com/search?pz=1&cf=vi_vn&hl=vi&q=MTP&gl=VN&ceid=VN:vi</link> <language>vi</language> <webMaster>news-webmaster@google.com</webMaster> <copyright>2020 Google Inc.</copyright> <lastBuildDate>Mon, 22 Jun 2020 10:51:31 GMT</lastBuildDate> <description>Google Tin tức</description> <item> <title>Sky Tour Movie của Sơn Tùng MTP tốp 7 Trending của Youtube - Báo Pháp Luật TP.HCM</title> <link>https://plo.vn/giai-tri/xem-nghe-doc/sky-tour-movie-cua-son-tung-mtp-top-7-trending-cua-youtube-917039.html</link> <guid isPermaLink="false">CBMia2h0dHBzOi8vcGxvLnZuL2dpYWktdHJpL3hlbS1uZ2hlLWRvYy9za3ktdG91ci1tb3ZpZS1jdWEtc29uLXR1bmctbXRwLXRvcC03LXRyZW5kaW5nLWN1YS15b3V0dWJlLTkxNzAzOS5odG1s0gEA</guid> <pubDate>Sat, 06 Jun 2020 07:00:00 GMT</pubDate> <description><a href="https://plo.vn/giai-tri/xem-nghe-doc/sky-tour-movie-cua-son-tung-mtp-top-7-trending-cua-youtube-917039.html" target="_blank" rel="noopener noreferrer">Sky Tour Movie của Sơn Tùng MTP tốp 7 Trending của Youtube</a> <font color="#6f6f6f">Báo Pháp Luật TP.HCM</font></description> <source url="https://plo.vn">Báo Pháp Luật TP.HCM</source> </item> <item> <title>Sky Tour của Sơn Tùng M-TP: Toàn kể thành công, cố quên thất bại? - Tuổi Trẻ Online</title> <link>https://tuoitre.vn/sky-tour-cua-son-tung-m-tp-toan-ke-thanh-cong-co-quen-that-bai-20200613100020661.htm</link> <guid isPermaLink="false">CBMiZ2h0dHBzOi8vdHVvaXRyZS52bi9za3ktdG91ci1jdWEtc29uLXR1bmctbS10cC10b2FuLWtlLXRoYW5oLWNvbmctY28tcXVlbi10aGF0LWJhaS0yMDIwMDYxMzEwMDAyMDY2MS5odG3SAQA</guid> <pubDate>Sat, 13 Jun 2020 07:00:00 GMT</pubDate> <description><a href="https://tuoitre.vn/sky-tour-cua-son-tung-m-tp-toan-ke-thanh-cong-co-quen-that-bai-20200613100020661.htm" target="_blank" rel="noopener noreferrer">Sky Tour của Sơn Tùng M-TP: Toàn kể thành công, cố quên thất bại?</a> <font color="#6f6f6f">Tuổi Trẻ Online</font></description> <source url="https://tuoitre.vn">Tuổi Trẻ Online</source> </item> <item>.....</item> <item>.....</item> <item>.....</item> </channel> </rss> |
II. Cấp quyền thư viện Retrofit 2
- Trang chủ: https://square.github.io/retrofit/
1. Implement thư viện
1 2 3 4 |
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } |
1 2 |
implementation 'com.squareup.retrofit2:retrofit:+' implementation 'com.squareup.retrofit2:converter-simplexml:+' |
2. Cấp quyền Internet
1 |
<uses-permission android:name="android.permission.INTERNET"/> |
III. Tạo các Class theo RSS
1. RssFeed.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.example.retrofitrssgoogle; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root(name = "rss", strict = false) public class RssFeed { @Element private RssChannel channel; public RssChannel getChannel() { return channel; } } |
2. RssChannel.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.example.retrofitrssgoogle; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; import java.util.ArrayList; import java.util.List; @Root(name = "channel", strict = false) public class RssChannel { @ElementList(inline = true, required = false) private ArrayList<RssItem> items; public ArrayList<RssItem> getItems() { return items; } } |
3. RssItem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.example.retrofitrssgoogle; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root(name = "item", strict = false) public class RssItem { @Element private String title; @Element private String link; @Element private String pubDate; @Element private String description; public String getTitle() { return title; } public String getLink() { return link; } public String getPubDate() { return pubDate; } public String getDescription() { return description; } @Override public String toString() { return title + '\n'; } } |
IV. Tạo class của Retrofit 2
1. interface RetrofitAPI.Class
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.example.retrofitrssgoogle; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; public interface RetrofitAPI { @GET("feeds?pz=1&cf=vi_vn&ned=vi_vn&hl=vi_vn") Call<RssFeed> getFeed( @Query("q") String q ); } |
2. RetrofitBuilder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.example.retrofitrssgoogle; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.converter.simplexml.SimpleXmlConverterFactory; public class RetrofitBuilder { private static RetrofitAPI retrofitAPI; public static RetrofitAPI getInstance(){ if (retrofitAPI == null){ Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://news.google.de/news/") .client(new OkHttpClient()) .addConverterFactory(SimpleXmlConverterFactory.create()) .build(); retrofitAPI = retrofit.create(RetrofitAPI.class); } return retrofitAPI; } } |
V. Sử dụng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
String key = "MTP"; RetrofitBuilder.getInstance().getFeed(key).enqueue(new Callback<RssFeed>() { @Override public void onResponse(Call<RssFeed> call, Response<RssFeed> response) { if (response.isSuccessful()){ RssFeed rssFeed = response.body(); RssChannel rssChannel = rssFeed.getChannel(); ArrayList<RssItem> items = rssChannel.getItems(); //items chính là List danh sách nhận được để dùng cho RecycleView Toast.makeText(MainActivity.this, items.toString(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Request Error", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call<RssFeed> call, Throwable t) { if (call.isCanceled()){ Toast.makeText(MainActivity.this, "Call was cancelled forcefully", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Network Error", Toast.LENGTH_SHORT).show(); } } }); |