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格式如下:
相关推荐
霸气的名字 2020-06-23
smaillift 2020-02-17
霸气的名字 2020-02-15
RikkaTheWorld 2019-12-08
TOmyhonour 2019-11-08
齐天大圣数据候 2019-10-31
kcstrong 2019-10-19
冰川孤辰 2019-09-08
kangtingting0 2019-09-07
俊光 2018-08-10
俊光 2019-07-30
xzw 2019-07-01
smaillift 2019-07-01
javashu0 2019-07-01
needh 2019-06-30
kcstrong 2019-06-28
qjbagu 2016-04-05
霸气的名字 2019-06-27