学习使用Retrofit 2在Android应用程序中解析sitemap (sitemap),使用简单的xml转换器依赖项进行xml解析。
在此示例中,我们将阅读并解析此博客的sitemap 。下面给出了一个示例条目:
$title(Sitemap)
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://leftso.com/retrofit2/retrofit-sync-async-calls/</loc>
<lastmod>2019-08-25T22:22:39+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
...
...
</urlset>
1.依赖性
为了能够在android中解析sitemap ,我们至少需要两个依赖项,即retrofit和converter-simplexml。
$title(build.gradle)
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.6.1'
compile 'com.squareup.retrofit2:converter-simplexml:2.6.1'
}
$title(pom.xml)
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-simplexml</artifactId>
<version>2.6.1</version>
</dependency>
2.sitemap 模型
首先创建用于使用sitemap 条目项的模型。
$title(SitemapResponse.java)
import java.util.ArrayList;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root(name = "urlset")
public class SitemapResponse
{
@ElementList(name = "url", inline = true)
private ArrayList<SitemapEntry> url;
@Override
public String toString() {
return "SitemapResponse [urlset=" + url + "]";
}
}
$title(SitemapEntry.java)
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="url")
public class SitemapEntry
{
@Element(name="loc")
private String loc;
@Element(name="lastmod")
private String lastmod;
@Element(name="changefreq")
private String changefreq;
@Element(name="priority")
private float priority;
@Override
public String toString() {
return "SitemapEntry [loc=" + loc + ", lastmod=" + lastmod + ",
changefreq=" + changefreq + ", priority=" + priority
+ "]";
}
}
3.sitemap 服务接口
创建服务接口,将通过改造来调用以执行HTTP请求。请注意API URL是sitemap.xml
。
$title(SitemapService.java)
import retrofit2.Call;
import retrofit2.http.GET;
public interface SitemapService
{
@GET("sitemap.xml") Call<SitemapResponse> getFeed();
}
4.读取sitemap 示例
让我们创建一个改造实例并执行sitemap 请求。给定示例使用异步请求示例。
$title(SitemapServiceDemo.java)
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;
public class SitemapServiceDemo
{
private static final String BASE_URL = "https://leftso.com/";
private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create());
private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static void main(String[] args) throws IOException
{
httpClient.addInterceptor(loggingInterceptor);
builder.client(httpClient.build());
Retrofit retrofit = builder.build();
SitemapService SitemapService = retrofit.create(SitemapService.class);
Call<SitemapResponse> callAsync = SitemapService.getFeed();
callAsync.enqueue(new Callback<SitemapResponse>() {
@Override
public void onResponse(Call<SitemapResponse> call, Response<SitemapResponse> response) {
if (response.isSuccessful()) {
SitemapResponse apiResponse = response.body();
// API response
System.out.println(apiResponse);
} else {
System.out.println("Request Error :: " + response.errorBody());
}
}
@Override
public void onFailure(Call<SitemapResponse> call, Throwable t) {
if (call.isCanceled()) {
System.out.println("Call was cancelled forcefully");
} else {
System.out.println("Network Error :: " + t.getLocalizedMessage());
}
}
});
}
}
程序输出。
$title(Console)
SitemapResponse [urlset=
[SitemapEntry [loc=https://leftso.com/
retrofit2/retrofit-sync-async-calls/,
lastmod=2019-08-25T22:22:39+00:00,
changefreq=monthly,
priority=0.8].
...
...
...
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/243711.html