Android学习和使用Retrofit框架
2017-03-01 08:49
134 查看
最近,有一款网络请求框架很火,那就是Retrofit框架(基于okhttp)。
首先,我们来看下我们在build.gradle中需要依赖的远端jar包
//retrofit框架
compile "com.squareup.retrofit2:retrofit:2.1.0"
compile "com.squareup.retrofit2:converter-gson:2.1.0"
compile "com.squareup.retrofit2:adapter-rxjava:2.1.0"
compile "com.squareup.okhttp3:logging-interceptor:3.4.1"
由于要做api开发,我们可以利用阿里云的免费数据~~(有好资源就要利用啊)
我们先新建个bean---Weather
public class Weather implements Serializable{
private String status;
private String msg;
private List<ResultBean> result;
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public class ResultBean{
private String cityid;
private String parentid;
private String citycode;
private String city;
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public String getParentid() {
return parentid;
}
public void setParentid(String parentid) {
this.parentid = parentid;
}
public String getCitycode() {
return citycode;
}
public void setCitycode(String citycode) {
this.citycode = citycode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
}
这里我是利用mvp+rxjava+retrofit+dagger2的开发
利用OOP,我们把要请求的接口封装起来 定义一个请求接口 ApiService
public interface ApiService {
//网络请求方法
@Headers("Autho
aa66
rization:APPCODE 你的appcode")
@GET("/weather/city")
Observable<List<Weather>> listRepos();
}
对接口实现(做具体的事情,我这里只是抽离出来)
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://jisutqybmf.market.alicloudapi.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService weatherService = retrofit.create(ApiService.class);
weatherService.listRepos().enqueue(new Callback<Weather>() {
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
Log.d("", "onResponse: " + response);
response.body().getResult();
return;
}
@Override
public void onFailure(Call<Weather> call, Throwable t) {
}
});
首先,我们来看下我们在build.gradle中需要依赖的远端jar包
//retrofit框架
compile "com.squareup.retrofit2:retrofit:2.1.0"
compile "com.squareup.retrofit2:converter-gson:2.1.0"
compile "com.squareup.retrofit2:adapter-rxjava:2.1.0"
compile "com.squareup.okhttp3:logging-interceptor:3.4.1"
由于要做api开发,我们可以利用阿里云的免费数据~~(有好资源就要利用啊)
我们先新建个bean---Weather
public class Weather implements Serializable{
private String status;
private String msg;
private List<ResultBean> result;
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public class ResultBean{
private String cityid;
private String parentid;
private String citycode;
private String city;
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public String getParentid() {
return parentid;
}
public void setParentid(String parentid) {
this.parentid = parentid;
}
public String getCitycode() {
return citycode;
}
public void setCitycode(String citycode) {
this.citycode = citycode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
}
这里我是利用mvp+rxjava+retrofit+dagger2的开发
利用OOP,我们把要请求的接口封装起来 定义一个请求接口 ApiService
public interface ApiService {
//网络请求方法
@Headers("Autho
aa66
rization:APPCODE 你的appcode")
@GET("/weather/city")
Observable<List<Weather>> listRepos();
}
对接口实现(做具体的事情,我这里只是抽离出来)
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://jisutqybmf.market.alicloudapi.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService weatherService = retrofit.create(ApiService.class);
weatherService.listRepos().enqueue(new Callback<Weather>() {
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
Log.d("", "onResponse: " + response);
response.body().getResult();
return;
}
@Override
public void onFailure(Call<Weather> call, Throwable t) {
}
});
相关文章推荐
- Android(OPhone) 学习笔记 - Mail API 的使用
- Android UI学习 - Tab的学习和使用 推荐
- APIDemo学习笔记——Android上几种简单的Animation使用方法(二)
- Mars Android视频学习笔记——01_16_SQLite使用方法
- Mars Android视频学习笔记——01_14/15_Handler的使用
- Android(OPhone) 学习笔记 - 搜索API的使用
- android学习----Handler使用
- Android学习之八:使用选择性widgets(using selection widgets)
- Android学习之七:使用Container
- OrmLite For Android 学习笔记 之一 Ormlite 介绍及使用
- Android UI学习 - GridView和ImageView的使用
- Android JNI学习之一 javah的使用
- Android(OPhone) 学习笔记3 - Mail API 的使用
- Android UI学习 - GridView和ImageView的使用
- Android学习之二:使用Android文档帮助
- Android(OPhone) 学习笔记 - 菜单使用
- Android(OPhone) 学习笔记2 - Home API 的使用
- android学习进行时----1.1.2UI部分Button使用-事件监听
- android学习小结5-各种控件使用方式DEMO
- Android UI学习 - Tab的学习和使用