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格式如下:
相关推荐
Retrofit2 使用Demo
实现retrofit的基础使用,如GET,POST请求,JSON解析,例子是获取歌曲,并且播放,其他歌曲内容省略,原理是一样不多做解析,适用于retrofit初学者。
前段时间花了点时间学了一下RxJava和Retrofit2,后面在实践中被没有用到,今天一看,呃,都基本上忘掉了。 在这里总结记录一下。
Retrofit 2.0使用详解,配合OkHttp、Gson,Android最强网络请求框架
这个Dmeo简单使用了Retrofit实现网络请求,对于想要学习Retrofit框架的人可以看看,很有帮助
简单的使用Retrofit2来请求数据
简单的retrofit demo,清楚明了,非常适合第一次接触retrofit的人
Flutter 快速配置retrofit实现后台数据获取,降低独自摸索的各种坑
RxJava 和 Retrofit 结合使用完成基本的登录和注册功能
Android实战——Retrofit2的使用和封装
MVP+Retrofit+RxAndroid使用demo
Android Retrofit2的简单使用,注释非常详细
里面介绍了retrofit的使用,样式很多欢迎下载。可以运用到项目中
retrofit的入门使用
详情请看http://blog.csdn.net/zxw136511485/article/details/78616577,
Retrofit2.0的学习以及Rxjava与Retrofit2的结合使用
简单的使用retrofit请求网络,获取数据。
Retrofit2基本使用,参数注解的基本使用,Path、Field、FieldMap、Body、Part、partMap、Query、QueryMap
一个网络请求封装库Retrofit 的简单使用
http://blog.csdn.net/yuankundong/article/details/51330468