retrofit使用SimpleXmlConverterFactory

这篇博客介绍了如何在Retrofit中使用SimpleXmlConverterFactory来处理XML格式的数据,包括配置步骤、实体类的创建和XML数据的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

retrofit 使用 SimpleXmlConverterFactory 解析 xml 格式数据

 

支持格式:

Gson	com.squareup.retrofit2:converter-gson:2.0.2
Jackson	com.squareup.retrofit2:converter-jackson:2.0.2
Moshi	com.squareup.retrofit2:converter-moshi:2.0.2
Protobuf	com.squareup.retrofit2:converter-protobuf:2.0.2
Wire	com.squareup.retrofit2:converter-wire:2.0.2
Simple XML	com.squareup.retrofit2:converter-simplexml:2.0.2
Scalars	com.squareup.retrofit2:converter-scalars:2.0.2

 

 

1配置:

 

    compile 'com.squareup.retrofit2:retrofit:2.0.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'
    compile ('com.squareup.retrofit2:converter-simplexml:2.0.1'){
        exclude group:'xpp3',module: 'xpp3'
        exclude group:'stax',module: 'stax-api'
        exclude group:'stax',module: 'stax'
    }

 2:根据对应的xml配置实体类 参考:http://blog.youkuaiyun.com/qqyanjiang/article/details/51200108

 

 3:

//配置打印日志
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build();

//      String baseUrl = "https://api.douban.com/v2/movie/";
        String baseUrl = "you url!!!";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .addConverterFactory(SimpleXmlConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        MovieService movieService = retrofit.create(MovieService.class);
//      Call<ResponseBody> call = movieService.login(0, 10);
        Call<LoginInfo> call = movieService.login("admin", "123456", "0");
        call.enqueue(new Callback<LoginInfo>() {
            @Override
            public void onResponse(Call<LoginInfo> call, Response<LoginInfo> response) {
                System.out.println(response.body().Table.USERNAME);
            }

            @Override
            public void onFailure(Call<LoginInfo> call, Throwable t) {
                t.printStackTrace();
            }
        });

4:创建类文件

 

 

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

public interface MovieService {

    @POST("Login")//这个是基于soap的post
    @FormUrlEncoded
    Call<LoginInfo> login(@Field("sLoginID") String start, @Field("sPassWord") String count, @Field("AppID") String AppID);
    //豆瓣电影
    @GET("top250")
    Call<ResponseBody> getMovie(@Query("start") int start, @Query("count") int count);
}

  

@Root(name = "NewDataSet", strict = false)
public class LoginInfo {
    @Element(name = "Table")
    public Table Table;
}

 

@Root(name = "Table", strict = false)
public class Table {
    @Element(name = "ID")
    public String ID;
    @Element(name = "ROLE")
    public String ROLE;
    @Element(name = "USERNAME")
    public String USERNAME;
}

 

 xml格式如下:



 

Retrofit ConvertFactory是一种用于将不同类型的数据转换的工具,常用于在Android应用程序中进行网络请求和数据处理。它是Retrofit库的一部分,可以方便地将不同类型的数据(例如JSON、XML、Protobuf等)转换为Java对象或反之。 Retrofit ConvertFactory通过实现Converter.Factory接口来实现数据转换。在创建Retrofit实例时,我们可以使用不同的ConvertFactory来定义我们需要的数据转换方式。 例如,我们可以使用GsonConverterFactory将JSON数据转换为Java对象,而在请求时将Java对象转换为JSON数据。这样可以方便地与服务器进行数据交互。 使用Retrofit ConvertFactory的主要步骤如下: 1. 创建Retrofit实例时,通过调用addConverterFactory方法添加所需的转换工厂。 2. 定义我们所需的数据转换方式,例如使用GsonConverterFactory。 3. 在定义API接口时,Retrofit会自动使用转换工厂对请求数据和响应数据进行转换。 4. 发起网络请求时,Retrofit会自动将请求数据转换为特定格式,例如将Java对象转换为JSON数据。 5. 接收到响应数据后,Retrofit会自动将响应数据转换为我们所需的格式,例如将JSON数据转换为Java对象。 通过使用Retrofit ConvertFactory,我们可以简化数据转换的过程,避免手动编写大量的数据处理代码。同时,Retrofit ConvertFactory还提供了其他常用的转换工厂,如SimpleXmlConverterFactory用于XML数据的转换,ProtoConverterFactory用于Protobuf数据的转换等。 总之,Retrofit ConvertFactory是一种强大的数据转换工具,能够方便地在Android应用程序中进行网络请求和数据处理,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值