Retrofit使用SimpleXmlConverterFactory

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.csdn.net/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格式如下:

image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容