Retrofit使用SimpleXmlConverterFactory

retrofit 使用 SimpleXmlConverterFactory 解析 xml 格式數(shù)據(jù)

支持格式:

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:根據(jù)對應(yīng)的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:創(chuàng)建類文件

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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容