Retrofit官网: https://github.com/square/retrofit
文档 http://square.github.io/retrofit/
随着Google对HttpClient 摒弃,和Volley的逐渐没落,OkHttp开始异军突起,而Retrofit则对okHttp进行了强制依赖。
Retrofit实质上就是对okHttp的封装
现在的Android开发者不会Retrofit + RXJava就感觉跟不上时代了一样,所以小试一下
转载请注明出处 ethan_xue博客
使用方法可查看官网。
按照下面步骤简单几步使用起来。
说明:涉及RXJava的用法直接跳到第5步
<uses-permission android:name="android.permission.INTERNET" />
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'io.reactivex:rxandroid:1.2.1'
public interface TestGetService {
@GET("adat/sk/{cityId}.html")
Call getWeather(@Path("cityId") String cityId);
}
Retrofit retrofit = new Retrofit.Builder()
//这里建议:- Base URL: 总是以/结尾;- @Url: 不要以/开头
.baseUrl("http://www.weather.com.cn/")
.build();
TestGetService apiStores = retrofit.create(TestGetService.class);
Call call = apiStores.getWeather("101010100");
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
try {
Log.e("xue", "response=" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call call, Throwable t) {
Log.e("xue", "onFailure=" + t.getMessage());
}
});
// 上面的天气:http://www.weather.com.cn/adat/sk/101010100.html
新建bean文件
public class WeatherJson {
//weatherinfo需要对应json数据的名称,我之前随便写了个,被坑很久
private Weatherinfo weatherinfo;
public Weatherinfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(Weatherinfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
//city、cityid必须对应json数据的名称,不然解析不了
public class Weatherinfo {
private String city;
private String cityid;
private String temp;
private String WD;
private String WS;
private String SD;
private String WSE;
private String time;
private String isRadar;
private String Radar;
private String njd;
private String qy;
//这里省略get和和set方法
}
}
修改Interface里的ResponseBody为WeatherJson
public interface TestGetService {
@GET("adat/sk/{cityId}.html")
Call getWeather(@Path("cityId") String cityId);
}
添加addConverterFactory(GsonConverterFactory.create())
Retrofit retrofit = new Retrofit.Builder()
//这里建议:- Base URL: 总是以/结尾;- @Url: 不要以/开头
.baseUrl("http://www.weather.com.cn/")
.addConverterFactory(GsonConverterFactory.create())
.build();
TestGetService apiStores = retrofit.create(TestGetService.class);
Call call = apiStores.getWeather("101010100");
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Log.e("xue", "getWeatherinfo=" + response.body().getWeatherinfo().getCity());
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
修改interface为
public interface TestGetService {
@GET("adat/sk/{cityId}.html")
Observable getWeather(@Path("cityId") String cityId);
}
增加.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
代码修改为
Retrofit retrofit = new Retrofit.Builder()
//这里建议:- Base URL: 总是以/结尾;- @Url: 不要以/开头
.baseUrl("http://www.weather.com.cn/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
TestGetService apiStores = retrofit.create(TestGetService.class);
Observable observable = apiStores.getWeather("101010100");
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(WeatherJson weatherJson) {
Log.e("mylog", weatherJson.getWeatherinfo().getCity());
}
});
这里是小试一下,没有做封装,封装可参考https://github.com/Tamicer/Novate
感觉代码比其他框架变多了,优点就是rxjava的优点
http://blog.csdn.net/liangdong131/article/details/51791034